Tuples, what are they: read-only lists or heterogeneous data arrays?

Nicola Larosa googlegroups at tekNico.net
Thu Mar 13 00:02:29 CET 2003


[WARNING: pet peeve ahead]

> FAQ entry 6.15 states:
> 
>> 6.15. Why are there separate tuple and list data types?
>> This is done so that tuples can be immutable while lists are
>> mutable.

There's more than that. The tutorial states:

"Tuples have many uses. For example: (x, y) coordinate pairs, employee
records from a database, etc."

http://www.python.org/doc/2.3a2/tut/node7.html#SECTION007300000000000000000

I was using MySQLdb, and naturally enough, it returns tuples. I built
another tuple with the values from one field for a number of records,
and I wanted the index of the record with a particular value, so I
reached for a tuple.index() method, and guess what? There is no such
thing. :^(

I went to Google and searched through c.l.py., and to my
disappointment, found these discussions:

PROP: tuple.index(), tuple.count()
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&safe=off&th=db5333ec65d2262c&rnum=1

How come tuples don't have an index method?
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&safe=off&th=99a50502426e8be5&rnum=1

which led to this Tracker entry:

[ 403693 ] tupleobject.c: implement tuple.index() and tuple.count()
http://sourceforge.net/tracker/?group_id=5470&atid=305470&func=detail&aid=403693

Date: 2001-02-15 05:49
Sender: gvanrossum
"Why bother?  I like the fact that when you want to do list-ish
things you have to use lists.  Use list(t).count(x) if you really
want this for a tuple.

"I'm rejecting this in the name of all those users who want Python
to be small."

Sorry, but this makes no sense. I entirely support the notion that a
tuple should be *exactly* like a list, apart from the fact that it is
immutable, so every read-only list method should be available for
tuples, too.

It's bad enough to be chided by Lua aficionados because they need just
one type to do what we do with tuples, lists and dictionaries. ;^) We
need no more superflous subtleties.

And no, list(t).index(x) does not qualify. Neither does
operator.indexOf() .

Guido, can't we settle this once and for all?




More information about the Python-list mailing list