On the console I typed in
>>> 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'>
I thought at first that the reason that d
only kept 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'm confused.
How come in the first code listing, d
only kept one pair, but in the second listing d
both keys were kept despite having same hash?