假设我有一个字典:
>>> d = {}
它有一个名为clear()
的方法:
>>> d.clear
<built-in method clear of dict object at 0x7f209051c988>
... 其中包含一个 __hash__
属性:
>>> d.clear.__hash__
<method-wrapper '__hash__' of builtin_function_or_method object at 0x7f2090456288>
...可调用的:
>>> callable(d.clear.__hash__)
True
那么为什么我不能对它进行哈希处理呢?
>>> hash(d.clear)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
注意: 我知道dict
对象是不可散列的 - 我想知道为什么这个限制扩展到它们的方法,即使如上所述,它们似乎声称否则?