spacing
spacing

Reputation: 780

Call tuple from outside function as dual variable in another function

I'm hitting a problem I don't seem to be able to solve in my algorith.

Let's say I have:

func1(x)

which returns a 2 element tuple, and I also have

func2(y,z)

which I want to use that tuple on. But everytime I try to call it on function2 as

func2(func1)

I get the error "missing 1 required positional argument:" because the function is receiving the tuple as:

func2((tuple1st_element, tuple2nd_element),) 

and not

func2(tuple1st_element, tuple2nd_element) 

How can I get it to do the latter?

Upvotes: 2

Views: 261

Answers (1)

user2555451
user2555451

Reputation:

Use the * argument unpacking syntax:

func2(*func1())

Below is a demonstration:

>>> def func1():
...     return 1, 2
...
>>> def func2(a, b):
...     return a + b
...
>>> func2(*func1())
3
>>>

Or, in simpler terms, doing this:

func(*(1, 2))

is equivalent to this:

func(1, 2)

Upvotes: 2

Related Questions