On the console, I typed
>>> class S(str): pass ... >>> a = 'hello' >>> b = S('hello') >>> d = {a:a, b:b} >>> d {'hello': 'hello'} >>> type(d[a]) <class '__main__.S'> >>> type(d[b]) <class '__main__.S'>
At first I thought that the reason d contained only one pair was because hash(a) and hash(b) returned the same values, so I tried:
>>> class A(object): ... def __hash__(self): ... return 0 ... >>> class B(object): ... def __hash__(self): ... return 0 ... >>> d = {A():A(),B():B()} >>> d {<__main__.A object at 0x101808b90>: <__main__.A object at 0x101808b10>, <__main__.B object at 0x101808d10>: <__main__.B object at 0x101808cd0>}
Now I am confused. How was only one pair stored in the first code list d , but in the second listing d both keys were saved, despite the same hash?
math4tots
source share