Death to tuples!

Steven Bethard steven.bethard at gmail.com
Mon Nov 28 22:15:44 EST 2005


Mike Meyer wrote:
> Steven Bethard <steven.bethard at gmail.com> writes:
> 
>>Dan Bishop wrote:
>>
>>>Mike Meyer wrote:
>>>
>>>
>>>>Is there any place in the language that still requires tuples instead
>>>>of sequences, except for use as dictionary keys?
>>>
>>>The % operator for strings.  And in argument lists.
>>>def __setitem__(self, (row, column), value):
>>>   ...
>>
>>Interesting that both of these two things[1][2] have recently been
>>suggested as candidates for removal in Python 3.0.
>>[1]http://www.python.org/dev/summary/2005-09-01_2005-09-15.html#string-formatting-in-python-3-0
>>[2]http://www.python.org/dev/summary/2005-09-16_2005-09-30.html#removing-nested-function-parameters
> 
> #2 I actually mentioned in passing, as it's part of the general
> concept of tuple unpacking. When names are bound, you can use a
> "tuple" for an lvalue, and the sequence on the rhs will be "unpacked"
> into the various names in the lvalue:
> 
>         for key, value = mydict.iteritems(): ...
>         a, (b, c) = (1, 2), (3, 4)
> 
> I think of the parameters of a function as just another case of
> this; any solution that works for the above two should work for
> function paremeters as well.

The difference is that currently, you have to use tuple syntax in 
functions, while you have your choice of syntaxes with normal unpacking::

py> def f(a, (b, c)):
...     pass
...
py> def f(a, [b, c]):
...     pass
...
Traceback (  File "<interactive input>", line 1
     def f(a, [b, c]):
              ^
SyntaxError: invalid syntax
py> a, (b, c) = (1, 2), (3, 4)
py> a, [b, c] = (1, 2), (3, 4)
py> a, [b, c] = [1, 2], (3, 4)
py> a, [b, c] = [1, 2], [3, 4]

Of course, the result in either case is still a tuple.  So I do agree 
that Python doesn't actually require tuples in function definitions; 
just their syntax.

STeVe



More information about the Python-list mailing list