Why are tuples immutable?

Antoon Pardon apardon at forel.vub.ac.be
Tue Dec 21 11:49:26 CET 2004

Op 2004-12-18, Roy Smith schreef <roy at panix.com>:
> Nick Coghlan <ncoghlan at iinet.net.au> wrote:
> [quoting from the Reference Manual]
>> 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)."
> I know that's what it says, but I don't think it's good advice.  All 
> that is really required is that __hash__() always returns the same value 
> over the lifetime of the object, and that objects which __cmp__() the 
> same always return the same hash value.  That's it.  That's all a 
> dictionary cares about.

It cares about even less. It only cares that these conditions are
met while the object is a key. If those things would change before
or after the object is a key, the dictionary wouldn't care about

Antoon Pardon

More information about the Python-list mailing list