Skip to main content
 首页 » 编程设计

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

2025年05月04日12telwanggs

<分区>

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

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' 

当然我可以做类似的事情

def one() : 
    return 3, 2 
def two(a, b): 
    return a + b 
a, b = one() 
two(a, b)