Death to tuples!
steven.bethard at gmail.com
Tue Nov 29 04:15:44 CET 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 have recently been
>>suggested as candidates for removal in Python 3.0.
> #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)):
py> def f(a, [b, c]):
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.
More information about the Python-list