Why are tuples immutable?

Roy Smith roy at panix.com
Sat Dec 18 15:04:29 CET 2004

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.

Making the object immutable is certainly the easiest way to achieve that 
goal, but it's not the only way.

More information about the Python-list mailing list