Tuples -- who needs 'em

Johann Hibschman johann at physics.berkeley.edu
Tue Apr 4 20:21:34 CEST 2000


Bob Alexander writes:

> I'm aware that this issue is not some great flash of insight. It is so
> obvious that the Python in-group certainly have given it plenty of thought.
> But that doesn't mean it's not worth talking about,

Like so many things...

In any case, I'll throw in my two cents.  Tuples and lists seem
conceptually much different to me, and I expect that they feel much
different to a lot of people, but I don't expect they feel that way to
everyone.  ;-)

Tuples are mathematical.  They have fixed lengths.  If python
variables are a space V, an N-tuple is V^N.  Tensor products, and all
that.

Lists are, well, lists.  I put things in them.  I take things out.
They've got nothing to do with tuples.

Tuples can be used to let functions return more than one value,
without the conceptual overhead of making a class.(1)  Lists are just
the wrong thing for that, they send the wrong message.  Tuples wrap up
elements from larger spaces, like points, (name, address) pairs, and
so on.

Ooh.  I think I hit my big reason.  Ignore the actual details of
typing, and so on.  I see lists as being mostly homogeneous,
regardless of their implementation.  They contain a list of names, or
numbers, or widgets.

I see tuples as combining different types into one conceptual whole.
(name, age) pairs.  (x, y) points.  (place, time).  They mentally bind
tighter than lists, turning other objects into new composite objects.
Lists just string them together.

Now python doesn't enforce this typing, but that's the way I think of
them.  And in that mindset, tuples are a completely natural thing.
Most applications could use lists instead of tuples, but there
wouldn't then be any way to write the more elementary concept of
"tuple-ness".  And I like having that concept around.

I like being able to write

  (x, y) = calculate_point(foo)
  a, b   = b, a
  name, sex, birthdate = unpack_info_string(str)

and so on.

  [x, y] = calculate_point(foo)
  [a, b] = [b, a]
  [name, sex, birthdate] = unpack_info_string(str)

just seem wrong.

Those aren't really technical arguments.  They're "expressiveness"
arguments.  And, to be honest, I have no idea if they convey the
point.  You may be right; it may be a religious issue.  (And I may be
crazy.  But it just might be a lunatic you're looking for.)  But, IMHO,
it's religion with style!


Cheers,

--Johann


1. By the way, C++ would be a much nicer language if it had a good way
   to return tuples without all the template cruft.  ML is your
   friend.

2. Also, the lisps make this distinction.  Common Lisp has values and
   multiple-value-bind, while R5RS Scheme now has values and, well,
   complicated macros to write multiple-value-bind.

-- 
Johann Hibschman                           johann at physics.berkeley.edu



More information about the Python-list mailing list