Re: Why are tuples immutable?

From: Steven Bethard (steven.bethard_at_gmail.com)
Date: 12/17/04


Date: Fri, 17 Dec 2004 09:19:46 GMT

jfj wrote:
>
> Why can't we __setitem__ for tuples?

It seems from your suggestions here that what you really want is a
single sequence type, list, instead of two sequence types: tuple and
list. Under your design, list would support hash, and it would be up to
the programmer to make sure not to modify a list when using it as a key
to a dict. The important thing to understand is that, while this is not
an unreasonable design decision, it's not the design decision that's
been made for Python.

Reading the docs and some of GvR's comments on python-dev, my
understanding is that, while a single sequence type would have sufficed,
the belief was that there were two mostly non-overlapping sets of tasks
that one might want to do with sequences. One set of tasks dealt with
"small collections of related data which may be of different types which
are operated on as a group"[1] (tuples), while the other set of tasks
dealt with "hold[ing] a varying number of objects all of which have the
same type and which are operated on one-by-one"[1] (lists).

Now, when you use a sequence as a key to a dict, you're operating on the
sequence as a group, not one-by-one. Given the above conceptions of
tuple and list then, it is natural to provide tuple with hash support,
while leaving it out of list.

Note also that tuples being immutable also falls out of the tuple
description above too. If you're operating on the sequence as a group,
then there's no need for __setitem__; __setitem__ is used to operate on
a sequence one-by-one.

I understand that you'd like a type that is operated on one-by-one, but
can also be considered as a group (e.g. is hashable). Personally, I
don't have any use for such a type, but perhaps others do. The right
solution in this case is not to try to redefine tuple or list, but to
write a PEP[2] and suggest a new datatype that serves your purposes.
I'll help you out and provide you with an implementation: =)

class hashablelist(list):
     def __hash__(self):
         return hash(tuple(self))

Now all you need to do is write the Abstract, Motivation and Rationale,
and persuade the people on c.l.py and python-dev that this is actually a
useful addition to the language.

Steve

[1]
http://www.python.org/doc/faq/general.html#why-are-there-separate-tuple-and-list-data-types
[2] http://www.python.org/peps/pep-0001.html



Relevant Pages

  • TOC of Python Cookbook now online (was Re: author index for Python Cookbook 2?)
    ... Processing a String One Character at a Time ... Finding a File on the Python Search Path ... Constructing Lists with List Comprehensions ... Looping over Items and Their Indices in a Sequence ...
    (comp.lang.python)
  • Re: Subclassing list the right way?
    ... about how overriding methods on them will affect other method calls. ... special interpretation of negative indexes (if the class wishes to ... outside the set of indexes for the sequence (after any special ... either write a lot of code or restrict the way you use lists to a ...
    (comp.lang.python)
  • Re: tuple.index()
    ... You can put any combination of things into both tuples and lists. ... tuple rather than a mutable sequence like a list. ... element is no real substitute for knowing its position. ... presence of an element is as good as knowing its position (since the ...
    (comp.lang.python)
  • Re: Newbie-Question concerning list and inheritance
    ... > OK, I'm hopefully a little less of a Lisp newbie these days, but Lisp's ... lists are simply built from cons cells. ... > list, sequence, t" ... > either, really, but I can kind of see why one might want to subclass ...
    (comp.lang.lisp)
  • Re: Lost info, need to bypass the password!
    ... > any windows password system... ... > rebooted and lost all info. ... Depending on the operating ... tell you what they are during the start up sequence. ...
    (microsoft.public.security)