python:我的类作为dict键.怎么样?

Ser*_*gey 9 python dictionary key

class A():
   def __init__(self, data=''):
       self.data = data  

   def __str__(self):
       return str(self.data)

d = {}  
elem = A()  
d[elem] = 'abc'  

elem2 = A()
print d[elem2]    # KeyError  
# actually elem2! was used not elem
Run Code Online (Sandbox Code Playgroud)

如何实现这一点而不会出错?

编辑:
FFFUUU,错误是:
我试图得到d[elem2](而不是elem)另一个A()BUT实例与相同的内容.(对我感到羞耻)
还是......我怎么能这样做?重新定义__hash__

Mic*_*zcz 13

答案是肯定的,你需要重新定义__hash__():

>>> class A(object):
...   def __init__(self, data=''):
...     self.data = data
...   def __eq__(self, another):
...     return hasattr(another, 'data') and self.data == another.data
...   def __hash__(self):
...     return hash(self.data)
... 
>>> a1, a2, a3 = A('foo'), A('foo'), A('bar')
>>> d = {a1: 'foo'}
>>> d[a1]
'foo'
>>> d[a2]
'foo'
>>> d[a3]
Traceback (most recent call last):
  File "", line 1, in 
KeyError: __main__.A object at 0x927d0>
Run Code Online (Sandbox Code Playgroud)

正如在另一个注释中所解释的那样,默认实现__hash__只是简单的身份,所以如果你想让它更复杂,你需要明确地定义它.

  • 您还需要实现`__eq__`,否则会发生奇怪的事情(至少在Python 2.7 中),我已经为您测试过了... (5认同)

Sve*_*ach 5

只要您不覆盖__hash__()__eq__()方法,您所做的就应该有效。它将使用对象标识作为相等。如果你想要一个不同的平等概念,你可以覆盖你的类的__hash__()__eq__()方法。