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

Thomas Wouters thomas at xs4all.net
Wed Mar 12 19:22:23 EST 2003


On Wed, Mar 12, 2003 at 03:02:29PM -0800, Nicola Larosa wrote:

[ Guido rejects the 'add-list-methods-to-tuples' patch ]

> 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.

You must have missed the posting that started this thread. Tuples *are not*
lists, and should not be used as 'read-only lists' for the sake of having a
read-only list or for the sake of performance. Tuples are more of a
light-weight, immutable object type than an actual list type; they're for
binding values closely together, as a single object. They convey a different
meaning to the reader of the program (or caller of a function): these value
are a single item (a response, an input parameter, a hostname and port, an
encoder/decoder pair) that consists of a (usually fixed) number of separate
items, and not necessarily of the same type. Related, but not welded
together. A port without the hostname is different information; you usually
need the hostname to make sense of the port at all. Et cetera.

Lists, on the other hand, convey the meaning of 'a set[*] of items', not
necessarily related in any way. A list of hostnames, for instance, could
well be built up out of just hostnames in string form or of tuples of
(hostname, port), and the hostnames could well be related by being in the
same domain, but you don't need one element of the list to make sense of the
other element.

Hence Guido's comment. If you have the need for a .count or .index method
for tuples, you are almost certainly using tuples the wrong way. No one is
going to stop you from writing a function to do the same thing on the
tuples, but the builtin datatype isn't going to change to accomodate misuse
of it.

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

He already did, that's why the patch is rejected.

He-just-didn't-settle-it-the-way-you-want-<wink>'ly y'rs
-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!





More information about the Python-list mailing list