General question about Python design goals

Mike Meyer mwm at mired.org
Thu Dec 1 02:37:49 EST 2005


"Donn Cave" <donn at drizzle.com> writes:
> 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 get all that, I really do. I would phrase it as "a tuple is a set of
attributes that happen to be named by integers." count doesn't make
sense on the attributes of an object - so it doesn't make sense on a
tuple. index doesn't make sense on the attributes of an object - so it
doesn't make sense on a tuple. A loop over the attributes of an object
doesn't make sense - so it doesn't make sense on a tuple.

So why the $*@& (please excuse my Perl) does "for x in 1, 2, 3" work?

Seriously. Why doesn't this have to be phrased as "for x in list((1,
2, 3))", just like you have to write list((1, 2, 3)).count(1), etc.?

      <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list