[Python-3000] __hash__ : Problem with either documentation or understanding

Nick Coghlan ncoghlan at gmail.com
Thu Jul 31 16:26:47 CEST 2008

Greg Ewing wrote:
> Nick Coghlan wrote:
>> Objects which compare equal must also end up in the same hash bucket 
>> in order for dictionaries to work correctly.
> And, if its equality with another object can change during
> its lifetime, it will never work properly in a dictionary.
> So in that case you should leave __hash__ unimplemented so
> that you can't accidentally use one as a dict key.

As of 2.6b2 (and 3.0b2) you can actually set "__hash__ = None" to 
explicitly block the inheritance of the method in a new-style class and 
have Python correctly consider the type unhashable even though it has a 
__hash__ attribute.

Making object.__hash__ inherited by default again in 2.6 while still 
preserving useful behaviour for the collections.Hashable ABC turned out 
to be a fairly interesting exercise...


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-3000 mailing list