Super Tuples

Justin Sheehy dworkin at ccs.neu.edu
Wed Dec 29 12:29:24 EST 1999


Paul Prescod <paul at prescod.net> writes:

> I agree that tuples in Python are often used merely as immutable lists.
> This bothers me

What else are they used as?

>  * why does Python need an immutable list type but not an immutable
> dictionary?

To name just the first example that comes to mind, tuples (because
they are immutable lists) make great dictionary keys.  I don't think
that an immutable dictionary type would be nearly as useful in that
sense.

>  * isn't immutability more a property of where an object is used than
> the objects actual type? For example, don't you sometimes want to pass a
> list to a function but guarantee that the list won't be modified?

No.  When you want that, you want to pass a tuple, not a list.  :-)

>  * it is because of this abuse that the "one-item tuple" problem arises
> so often.

Which abuse?

> In Python world we most often use tuples as mathematicians do.

>From what I have seen, people most often use Python tuples as
immutable lists, since that is essentially what they are.  This use
happens to very frequently overlap with the mathematical sense of a
tuple.  

> If you use tuples as mathematicians do then single-item tuples can never
> arise because senders and recievers would never agree on a "protocol"
> (used loosely) that involves 1 length tuples (why use a tuple!).

Surely, creating a 1-item tuple to begin with rarely makes sense.
Also, the syntax for doing so is admittedly ugly.  However, there are
plenty of situations where one-item tuples can end up occurring as a
result of data handling.

For instance, a function may receive a tuple of arbitrary size.  (Yes,
I know that this goes against your personal notion of a tuple, but it
makes plenty of sense in Python terms.)  That function may break down
the data in the tuple in a recursive manner, which can certainly lead
to one-item tuples.  What is the big deal with that?

It really sounds like what you are asking for is a lot more like a
dictionary than a tuple.  It sounds like you should be lobbying for a
new mapping type, not a change to the existing (and very useful) tuple.

Or is the problem simply that the word "tuple" evokes for you
something different than what Python calls by that name?
It really sounds like all of the uses which you name for these Super
Tuples would be solved quite well by a dictionary, or perhaps a
lockable dictionary.  

The primary virtue of tuples is that they are simple, immutable sequences.

I see no benefit in removing any of their simplicity.  

-Justin

 



More information about the Python-list mailing list