Why are tuples immutable?

Jeff Shannon jeff at ccvcorp.com
Thu Dec 16 03:30:01 CET 2004

Antoon Pardon wrote:

>Demanding that users of dictioanaries somehow turn their mutable objects
>into tuples when used as a key and back again when you retrieve the keys
>and need the object [...]

But, you generally don't "retrieve" _keys_ from dicts.  You *use* keys 
to retrieve *values* from a dict.  The only way to get a dict to give 
you a key is by using the keys() method (or items() method) to give you 
*all* of the keys. 

You say that mutating the keys inside of a dict is bad, and yet you want 
keys to be mutable.  This seems to be an internally inconsistent 
position, nevermind the fact that I can't think of a case where I'm 
likely to "retrieve" a key from a dict, modify it, and then put it 
back.  (I can think of endless cases where I'd want to do that with 
*values*, but not with keys...)

(And if dicts were somehow changed so that keys were copied when added 
to a dict, just so that every once in a while someone might be able to 
avoid a few conversions to/from tuple, then you're adding the overhead 
of an object copy to *every* dict insertion, thus slowing down (possibly 
by a significant amount) a large proportion of Python operations.  How 
is that a gain?)

Jeff Shannon
Credit International

More information about the Python-list mailing list