1

I have a function that has 4 parameters defined as:

call(a,b,c,d):

I have another function called

return_args() #that returns 4 variables. 

When I do the following:

call(return_args()) #it errors out saying, that's 1 arguments, not 4 arguments 

I know I can store the 4 variables in another line previous to the call(), but it it possible to return the variables all within call() line.

4 Answers 4

4

You can use * argument unpacking

call(*return_args())

This is a shorthand form of the following:

a, b, c, d = return_args()
call(a, b, c, d)

or even

tup = return_args()
call(*tup)

Python's * operator used in this way essentially says "unpack this tuple and pass the unpacked values to this function as arguments".

The related operator ** does a similar trick for keyword arguments:

def call(arg1=None, arg2=None, **kwargs):
    pass

kwargs = {'arg1': 'test', 'arg3': 'whatever'}
call(**kwargs)

* and ** can be used together:

def call(*args, **kwargs):
    pass
call(*return_args(), **kwargs)
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the * operator to send the list as separate arguments:

call(*return_args())

Comments

1

Use the 'variable unpacking' syntax: call( *return_args() )

Alternatively you can use an obsolete syntax: apply( call, return_args() )

Comments

1
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 )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.