2

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?

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

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.