Why are tuples immutable?

Steve Holden steve at holdenweb.com
Mon Dec 13 08:08:15 EST 2004


jfj wrote:

> 
> Yo.
> 
> Why can't we __setitem__ for tuples?
> The way I see it is that if we enable __setitem__ for tuples there
> doesn't seem to be any performance penalty if the users don't use it
> (aka, python performance independent of tuple mutability).
> 
> On the other hand, right now we have to use a list if we want to
> __setitem__ on a sequence. If we could use tuples in the cases where
> we want to modify items but not modify the length of the sequence,
> programs could be considerably faster. Yes?
> 
> Enlighten me.
> 
> G.
> 
Well, the best answer may be "tuples are immutable because otherwise 
there wouldn't be an immutable sequence type".

There have been many discussions on this topic (not that I'm blaming you 
for being unaware of them), and normally when someone raises this topic 
there's a flurry of nbew justifications for the existing behavior. The 
bottom line seems to be that Guido wanted something that corresponds to 
a mathematical tuple, which is an ordered collection of items of 
(potentially) different types.

Of course there's nothing much to stop you using lists instead of 
tuples, except the rare piece of code that insists on one or the other.

regards
  Steve
-- 
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119



More information about the Python-list mailing list