Tuples -- who needs 'em

Warren Postma embed at geocities.com
Mon Apr 3 15:50:31 EDT 2000


> How does having both types significantly improve life for Python
> programmers?

I don't want to reopen the can of worms, but here's how I think of it,
conceptually:

First, every time you invoke a function:

    f(x,y,z)

- Python looks in it's namespace(s) for the function f, this is a python
object
- Python creates a Tuple of the arguments you passed, ie, args=(x,y,z)
- Python invokes the function internally like this: Apply(f,args)

So, Tuples are an unavoidable concept inside the construction of Python. The
only
remaining question is, what value is there in exposing them to the user.



Well, the first value to me, is that Python by nature allows introspection
to an unprecedented degree. That is the most powerful thing, to me, about
Python programming. I can separate a function name from the parameters,
store them some place, and combine them, chemistry-set style, using map and
apply.



Nice Uses of Tuples-"The Point":

A point consists of X,Y point pairs. This is a natural use for a Tuple. If
the file format was 3D, you'd have an X,Y,Z tuple.  As a whole, the tuple
represents a single point in space. It is no longer two unrelated integers.
It's a literal-value with a presence in two-space or three-space, if you
like to think of things that way.  Of course, you can't change part of a
Tuples, any more than you can change Part of the number 3.  It's either 3 or
it's not, and it's a point at (5,10) or it's not.  If you still don't get
the Immutable thing, you need to meditate some more on the Way of the
Pythonista.  Read the Manuals, while chanting the Newbie Mantra: "Hmmm".


Lists + Tuples: "Sequence of Points"

Imagine putting the tuples (point pairs) together into a List of points. A
list is the appropriate type, because you can insert and delete items from a
list, something which would be a Bad Idea in Tuples, because they are
Immutable.

The list of tuples gives you the best of both worlds. Variable length in the
list, so you can have any number of Points in the list, but you can't go
adding a third item to a Tuple once created, nor delete the second item.
You can pass this tuple around the system by reference and it will never get
Messed With either.  If someone changes their own local copy, they get a new
tuple just for them.

So without the distinction, Python's "object reference" scheme would be
horribly broken.


Dictionaries + Tuples: "Point to Name Map"

Suppose now you are mapping Points on a Map to City Names. A map could be
large, and it might be a bad idea to create a 2 dimensional array.   You
can't use a List as the Key to a dictionary, and here's an example of where
a Tuple is just the ticket:

CityName = {} # empty dictionary
CityName[(5,24)] = 'London'
CityName[(7,23)] = 'Paris'
CityName[(9,24)] = 'Berlin'

A dictionary using a Tuple as the key is a nice way to do some kinds of
Sparse arrays.


Tuples in Tuples: "Gettin' Jiggy with Recursion"

Tuples nested inside Tuples can be useful as well.  You can use them to do
certain recursive types of things nicely:

from types import *
b = ( 9, "*", (7, "-", 2)) # infix sequence of operations as a tuple
def f(x):
  if (type(x)!=TupleType):
    return x
  a,b,c = x
  v = str(f(a))+b+str(f(c))
  print v
  return eval( v )

print f(b)
#output:
#
# 7-2
# 9*5
# 45


Warren









More information about the Python-list mailing list