Super Tuples

Magnus L. Hetland mlh at vier.idi.ntnu.no
Thu Dec 30 09:49:45 EST 1999


Paul Prescod <paul at prescod.net> writes:

> Eugene Goodrich wrote:
> > 
> > When I started in Python, my PyMentor described tuples as immutable
> > lists.  When they start acting like much more than that, isn't it time
> > to use an object?
> 
> I agree that tuples in Python are often used merely as immutable lists.
> This bothers me for several reasons:
> 
>  * that isn't what "tuple" means in mathematics. In most mathematics
> EVERYTHING is immutable. Tuples and lists are still distinct

Yes. More about definitions below.

>  * it is because of this abuse that the "one-item tuple" problem arises
> so often. There is no such problem in mathematics because a one-item
> tuple does not make sense.

A common definition of an ordered set or tuple is like this:

- The ordered pair (x, y) is defined as the set {{x},{x,y}}
- An n-tuple (a,b,...,z) is defined as ((a,b,...,y),z)

This definition obviously doesn't define tuples of length 1. I'm not
sure that means that they are meaningless, though. It would be simple
to extend the definition if you needed it, as for instance:

  (x) = {x}

The difference between tuples and lists are that lists (or sequences)
often are defined as partial functions with domain N0; for instance:

  s = <a,b,c,d>

Then

  s(0) = a, s(1) = b etc.

They may be used in different contexts, but really, they aren't all
that different.


> In Python world we most often use tuples as mathematicians do.
> Really, a tuple is supposed to be a collection of things where the
> sender and the recieiver have agreed on the number of items in
> advance, and every item has a special "meaning." The time library is
> a perfect example of this:

Yes - I guess that is the most common use. (As in cartesian points...
Even though the dimentions may not be all that different, they are at
least defined by their position...)

> The primary weakness with the time library is that you have to index
> daylight savings flag by (for example) [9] instead of .daylight .

Well - the way this is done in mathematics is usually (in my
experience) something like this:

  We have a graph G=(V,E) where V is the set of vertices and E is the
  set of edges...

The elements of the tuple G are not named - it is unpacked, just like
in Python:

  We have a graph (V,E)=G where V is the (e.g.) list of vertices, and
  E is the (e.g.) dictionary of edges.

Or something.

> If you use tuples as mathematicians do then single-item tuples can
> never arise because senders and recievers would never agree on a
> "protocol" (used loosely) that involves 1 length tuples (why use a
> tuple!).

I guess these things in Python have a lot to do with the underlying
C-implementation, since tuples in many circumstances are quite
efficient, and one-element tuples may be used to be consistent
sometimes.

> 
>  Paul Prescod
> 

--

  Magnus
  Lie
  Hetland



More information about the Python-list mailing list