Semantics of tuple unpacking in python

iterable-unpacking, python, tuples

Solution

i am pretty sure that the reason people "naturally" don't like this is because it makes the meaning of later arguments ambiguous, depending on the length of the interpolated series:

def dangerbaby(a, b, *c):
    hug(a)
    kill(b) 

>>> dangerbaby('puppy', 'bug')
killed bug
>>> cuddles = ['puppy']
>>> dangerbaby(*cuddles, 'bug')
killed bug
>>> cuddles.append('kitten')
>>> dangerbaby(*cuddles, 'bug')
killed kitten

you cannot tell from just looking at the last two calls to `dangerbaby` which one works as expected and which one kills little kitten fluffykins.

of course, some of this uncertainty is also present when interpolating at the end. but the confusion is constrained to the interpolated sequence - it doesn't affect other arguments, like `bug`.

[i made a quick search to see if i could find anything official. it seems that the * prefix for varags was introduced in python 0.9.8. the previous syntax is discussed here and the rules for how it worked were rather complex. since the addition of extra arguments "had to" happen at the end when there was no * marker it seems like that simply carried over. finally there's a mention here of a long discussion on argument lists that was not by email.]

Problem

Why does python only allow named arguments to follow a tuple unpacking expression in a function call? ``` >>> def f(a,b,c): ... print a, b, c ... >>> f(*(1,2),3) File "<stdin>", line 1 SyntaxError: only named arguments may follow *expression ``` Is it simply an aesthetic choice, or are there cases where allowing this would lead to some ambiguities?

Original source