使用具有多个结果的函数作为参数

dr *_*rry 1 python function parameter-passing

我有一个返回多个值的函数,我能以某种方式直接在另一个函数的参数列表中使用该函数吗?当我尝试(天真地)时,我得到:

def one() :
    return 3, 2
def two(a, b):
    return a + b
two(one())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-86-27980b86a2c0> in <module>()
  3 def two(a, b):
  4     return a + b
----> 5 two(one())

TypeError: two() missing 1 required positional argument: 'b'
Run Code Online (Sandbox Code Playgroud)

当然我可以做点什么

def one() :
    return 3, 2
def two(a, b):
    return a + b
a, b = one()
two(a, b)
Run Code Online (Sandbox Code Playgroud)

ffe*_*rri 6

当然.

two(*one())
Run Code Online (Sandbox Code Playgroud)

* 是参数解包

有用的阅读:理解Python的星号(*)