Why are tuples immutable?

Nick Coghlan ncoghlan at iinet.net.au
Sat Dec 18 05:26:27 CET 2004

Antoon Pardon wrote:
> Would you have us construct two related classes each time we find
> ourselves in such a situation and copy an object from one
> class to the other depending on the circumstances?

Python itself seems to think so, given the pairings of set/frozenset & list/tuple.

Using genuinely immutable objects as dictionary keys is much easier than saying 
"while this object is part of a dictionary, don't alter it's hash value or 
comparison results". Instead, the immutable version is provided to say 
"alterations are not allowed on this copy"

You can certainly *do* the former (using __hash__ and appropriate comparison 
overrides), but it isn't particularly easy to do correctly, and hence usually 
isn't a great idea unless copies are *really* expensive (and even then, a 
shallow copy approach can often suffice).


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

More information about the Python-list mailing list