python - 为什么 sets,dicts,list 在 python 中是不可散列的

标签 python hash set

不可哈希到底是什么意思?

>>> 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/

相关文章:

python - Tornado POST 405 : Method Not Allowed

python 复制带有元数据的文件,shutil 与手动打开和写入重复文件

python - 如何在Ubuntu 14.0.4上安装ImageHash?

java - 自定义哈希算法

security - 如何正确应用客户端密码哈希?

Java : Iterating a Set in ibatis

java - 将 HashMap 添加到 TreeSet 的奇怪行为

Python 装饰器访问自身

python - 我如何找到 Pyramid 中 URI 中的 anchor ?

c - 在c中设置基数