We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

The __hash__ method


Called for the key object for dictionary operations, and by the built-in function hash. Should return a 32-bit integer usable as a hash value for dictionary operations. The only required property is that objects which compare equal have the same hash value; it is advised to somehow mix together (e.g., using exclusive or) the hash values for the components of the object that also play a part in comparison of objects.

If a class does not define a __cmp__ method it should not define a __hash__ operation either; if it defines __cmp__ or __eq__ but not __hash__, its instances will not be usable as dictionary keys. If a class defines mutable objects and implements a __cmp__ or __eq__ method, it should not implement __hash__, since the dictionary implementation requires that a key’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).