不可哈希到底是什么意思?
>>> a={1,2,3}
>>> b={4,5,6}
>>> set([a,b])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
>>>
谁能说出错误到底是什么?我还可以在 python 中将集合添加到另一个集合中吗?
最佳答案
没有 __hash__()
的对象名为 unhashable
的属性。 Python 文档已经很好地描述了原因:
If a class defines mutable objects and implements an
__eq__()
method, it should not implement__hash__()
, since the implementation of hashable collections requires that a key’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).
关于python - 为什么 sets,dicts,list 在 python 中是不可散列的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35811046/