"TypeError: unhashable type" during lookup of unhashable object in dict/set

"TypeError: unhashable type" during lookup of unhashable object in dict/set

前言

我知道 dicts/sets 应该 created/updated 只是因为它们的实现,所以当这种代码失败时

>>> {{}}  # empty dict of empty dict
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unhashable type: 'dict'

没关系,我已经看到很多这样的消息了。

但是如果我想检查 set/dict

中是否有一些无法散列的对象
>>> {} in {}  # empty dict not in empty dict

我也报错

Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unhashable type: 'dict'

问题

这种行为背后的基本原理是什么?我知道查找和更新可能在逻辑上是相关联的(如 dict.setdefault method),但它不应该在修改步骤而不是查找时失败吗?也许我有一些我以某种方式处理的可散列 "special" 值,但其他的(可能是不可散列的)——在另一个:

SPECIAL_CASES = frozenset(range(10)) | frozenset(range(100, 200))
...
def process_json(obj):
    if obj in SPECIAL_CASES:
        ...  # handle special cases
    else:
        ...  # do something else

所以对于给定的查找行为,我不得不使用其中一个选项

还是我遗漏了一些微不足道的东西?

您肯定已经意识到,set 和 dicts 的内部工作原理非常相似。基本上这个概念是你有键-值对(或只是带有一组的键),并且键永远不能改变(不可变)。如果一个对象是可变的,那么散列将失去它作为底层数据的唯一标识符的意义。如果您无法判断一个对象是否唯一,那么一组唯一键的含义就失去了它的唯一性键属性。这就是为什么可变类型在集合中和作为字典的键是不允许的。以你的例子: {} in {} # empty dict not in empty dict 我认为你有点误解,因为 dict.__contains__ 只检查字典的键,而不检查值。因为你永远不能将字典作为键(因为它是可变的)这是无效的。

我在 Python 错误跟踪器上找到了 this issue。长话短说:

如果

>>> set([1,2]) in {frozenset([1,2]): 'a'}

返回 False 它在某种程度上是违反直觉的,因为值是相等的

>>> set([1,2]) == frozenset([1,2])
True

所以我想我会编写和使用适当的实用程序,以应对可能发生这种情况的情况。


关于错误的根源:在 CPython repo dict___contains__ function (which is a dict.__contains__ method implementation) calls PyObject_Hash function (which corresponds to hash function) -> for unhashable objects (like {} in our first case) calls PyObject_HashNotImplemented function -> 中产生此错误。