dict duplicity

Steven Bethard steven.bethard at gmail.com
Thu Aug 18 22:29:43 EDT 2005


Randy Bush wrote:
>    for pKey, pVal in dict.iteritems():
>       print \
>          pKey[0], hash(pKey[0]), \
>          pKey[1], hash(pKey[1]), \
>          pKey[2], hash(pKey[2]), \
>          "hash=", hash(pKey), \
>          pVal[0], hash(pVal[0]), \
>          pVal[1], hash(pVal[1])
> 
> when run with | sort, produces
> 
> 12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 917088000 917088000
> 12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 917088000 917088000
> 12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 917088000 917088000
> 12.0.0.0 -2054516913 8 8 7018 329707286 hash= -604503432 917088000 917088000 917088000 917088000
> 12.0.0.0 -2054516913 9 -1293912648 7018 329707286 hash= -1578430040 917088000 917088000 917088000 917088000
> 12.0.0.0 -2054516913 9 -1293912648 7018 329707286 hash= -1578430040 917088000 917088000 917088000 917088000
> 
> not that there are two entries with the same hash=

Whatever the hash function is for your keys, it returns the same value 
for two different keys.  A simple example of this kind of phenomenon:

py> class C(object):
...     def __hash__(self):
...         return 1
...
py> c1, c2 = C(), C()
py> d = {c1:1, c2:2}
py> for key, value in d.iteritems():
...     print hash(key), key, value
...
1 <__main__.C object at 0x0126CCD0> 1
1 <__main__.C object at 0x012739D0> 2

Note that the hashes are the same.  But are the objects the same?

py> c1 == c2
False

Nope.  (This is because the default __eq__ method compares the ids of 
the two objects, and as you can see, they have different ids.)

The point is this: you can have multiple objects in the same dict that 
have the same hash(), as long as none of those objects equals another. 
For more information, see:
     http://wiki.python.org/moin/DictionaryKeys

STeVe



More information about the Python-list mailing list