问题描述:

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?

网友答案:

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)
相关阅读:
Top