Tuple Semantics - Rationale'?

Skip Montanaro skip at pobox.com
Wed Jul 11 16:35:15 EDT 2001


    Tim> From what I've read, to force the data structure to be maintained
    Tim> correctly, I have to add a trailing comma to the single arg pair
    Tim> like this:

    Tim>       )
    Tim>        (name-n, descrip-n),
    Tim>        ( (argA-1, argB-1), )
    Tim>       ),


That's because the construct

    ( (argA-1, argB-1) )

without the comma can't be distinguished syntactically from simply wrapping
parens around the inner value.

Consider a simpler example:

    if (a) == 5:

vs.

    if (a,) == 5:

The first is simply parenthesizing a variable and comparing it to the number
5.  The second is creating a one-element tuple and comparing it to the
number 5.

The second variant could be written as

    if a, == 5:

but in my mind that's hiding the tuple creation a bit.

For more discussion, check the Tuples and Sequences section of the Python
tutorial:

    http://www.python.org/doc/current/tut/tut.html

-- 
Skip Montanaro (skip at pobox.com)
(847)971-7098




More information about the Python-list mailing list