return_args() #that returns 4 variables.
This is a misunderstanding, it doesn't return 4 variables. A function like:
def return_args():
return 1, 2, 3, 4
Is actually doing this:
def return_args():
mytuple = (1, 2, 3, 4)
return mytuple
and returning a single thing.
Another side of this is "destructuring assignment" in Python, which is the ability to do this:
a, b = 1, 2
It's a way to assign/bind two variables at once, but it's actually creating then unpacking the sequence (1,2)
. You can write:
a, b, c, d = return_args()
and it looks like you returned four things and bound them to four variable names and that's a clean, useful abstraction, but that's not what happened - actually one sequence was created (with 4 things in it), then it was unpacked to match a sequence of variable names.
The two abstractions leak, and you find out that return_args() is returning a single thing when you try to do this:
call(return_args()) #it errors out saying, that's 1 arguments, not 4 arguments
The other answers are rightly suggesting call(*return_args())
as one solution, it's documented here under "Unpacking argument lists": http://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists
(The other side to this is a function created to accept variable numbers of arguments discussed here: https://stackoverflow.com/a/11550319/478656 )