General question about Python design goals

Donn Cave donn at drizzle.com
Thu Dec 1 02:13:44 EST 2005


Quoth bonono at gmail.com:
| Christoph Zwerschke wrote:
...
|> Sorry, but I still do not get it. Why is it a feature if I cannot count
|> or find items in tuples? Why is it bad program style if I do this? So
|> far I haven't got any reasonable explanation and I think there is no.
|
| I have no idea, I can understand their view, not necessarily agree. And
| reasonable explanation is not something I usually find on this group,
| for issues like this.

It's hard to tell from this how well you do understand it, and of
course it's hard to believe another explanation is going to make
any difference to those who are basically committed to the opposing
point of view.  But what the hell.

Tuples and lists really are intended to serve two fundamentally different
purposes.  We might guess that just from the fact that both are included
in Python, in fact we hear it from Guido van Rossum, and one might add
that other languages also make this distinction (more clearly than Python.)

As I'm sure everyone still reading has already heard, the natural usage
of a tuple is as a heterogenous sequence.  I would like to explain this
using the concept of an "application type", by which I mean the set of
values that would be valid when applied to a particular context.  For
example, os.spawnv() takes as one of its arguments a list of command
arguments, time.mktime() takes a tuple of time values.  A homogeneous
sequence is one where  a  and  a[x:y]  (where x:y is not 0:-1)  have
the same application type.  A list of command arguments is clearly
homogeneous in this sense - any sequence of strings is a valid input,
so any slice of this sequence must also be valid.  (Valid in the type
sense, obviously the value and thus the result must change.)  A tuple
of time values, though, must have exactly 9 elements, so it's heterogeneous
in this sense, even though all the values are integer.

One doesn't count elements in this kind of a tuple, because it's presumed
to have a natural predefined number of elements.  One doesn't search for
values in this kind of a tuple, because the occurrence of a value has
meaning only in conjunction with its location, e.g., t[4] is how many
minutes past the hour, but t[5] is how many seconds, etc.

I have to confess that this wasn't obvious to me, either, at first, and
in fact probably about half of my extant code is burdened with the idea
that a tuple is a smart way to economize on the overhead of a list.
Somewhere along the line, I guess about 5 years ago? maybe from reading
about it here, I saw the light on this, and since then my code has gotten
easier to read and more robust.  Lists really are better for all the
kinds of things that lists are for -- just for example, [1] reads a lot
better than (1,) -- and the savings on overhead is not worth the cost to
exploit it.  My tendency to seize on this foolish optimization is however
pretty natural, as is the human tendency to try to make two similar things
interchangeable.  So we're happy to see that tuple does not have the
features it doesn't need, because it helps in a small way to make Python
code better.  If only by giving us a chance to have this little chat once
in a while.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list