* for generic unpacking and not just for arguments?

Russell Warren russandheather at gmail.com
Sun Nov 29 09:25:24 EST 2009


Is there a reason that this is fine:

>>> def f(a,b,c):
...   return a+b+c
...
>>> f(1, *(2,3))
6

but the code below is not?

>>> x = (3, 4)
>>> (1, 2, *x) == (1, 2, 3, 4)
Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
invalid syntax: <string>, line 1, pos 8

Why does it only work when unpacking arguments for a function?  Is it
because the code below is preferred, and more readable?

>>> x = (3, 4)
>>> (1, 2) + x == (1, 2, 3, 4)
True

I've rooted around to see if there is an answer already and found some
threads going way back to 1998 (!!), but can't find a concise answer
as to why it is limited to args.

I don't have a burning desire for this to work, but I just tried it
unsuccessfully when building up a tuple and was mildly surprised that
it didn't work, so I'm curious why.

Maybe it's just that * is strictly for arguments, and trying it for
generic tuple unpacking is abuse (which is down the corridor in 12A).



More information about the Python-list mailing list