dictionary keys, __hash__, __cmp__
python at meyer-luetgens.de
Tue Nov 4 21:52:42 CET 2003
In the Python Language Reference, I found the following
statements about using objects as dictionary keys:
1. "__hash__() should return a 32-bit integer."
2. "The only required property is that objects which
compare equal have the same hash value."
3. "If a class does not define a __cmp__() method it
should not define a __hash__() operation either."
Can I asume that:
-- it is guaranteed that id(obj) returns a unique 32-bit integer
-- keys are interchangeable (equivalent),
if the following is valid:
hash(key1) == hash(key2) and key1 == key2
-- I can ignore the 2nd statement, if I am aware of
the fact that: if objects are equal it dosn't mean that
they are the same key.
-- I can savely ignore the 3rd statement, because python
falls back to cmp(id(obj1), id(obj2)), if __cmp__()
is not defined.
More information about the Python-list