最佳答案
Can anyone explain why passing a generator as the only positional argument to a function seems to have special rules?
If we have:
def f(*args):
print "Success!"
print args
This works, as expected.
>>> f(1, *[2])
Success!
(1, 2)
This does not work, as expected.
>>> f(*[2], 1)
File "<stdin>", line 1
SyntaxError: only named arguments may follow *expression
This works, as expected
>>> f(1 for x in [1], *[2])
Success!
(generator object <genexpr> at 0x7effe06bdcd0>, 2)
This works, but I don't understand why. Shouldn't it fail in the same way as 2)
>>> f(*[2], 1 for x in [1])
Success!
(generator object <genexpr> at 0x7effe06bdcd0>, 2)