【发布时间】:2016-05-29 23:48:27
【问题描述】:
我搜索了 SO 并发现了很多关于这个主题的问题,我尝试重新创建已接受的答案,但我无法复制显示的输出。
>>> class testFoo(object):
... def __init__(self,x):
... self.x = x
... def __eq__(self, other):
... return hash(self.x) == other
... def __hash__(self):
... return hash(self.x)
...
>>> d = {}
>>> x = testFoo("a")
>>> d[x] = 1
>>> d
{<__main__.testFoo object at 0x7f6d9ccc5550>: 1}
>>> hash(x)
12416037344
>>> hash("a")
12416037344
>>>
当我在上面键入“d”时,我期望看到的是键“a”,而不是对象的 repr 字符串。我做错了什么?
【问题讨论】:
标签: python dictionary hash key built-in