Skip to main content
 首页 » 编程设计

python之将函数调用作为函数参数传递

2025年05月04日34itcoder

代码:

def function1(a,b): 
    return a-1,b-1 
def function2(c,d): 
    return c+1,d+1 
 
print function1(function2(1,2))    

错误:

Traceback (most recent call last): 
  File "C:\Users\sony\Desktop\Python\scripts\twitter_get_data.py", line 6, in <module> 
    print function1(function2(1,2))     
TypeError: function1() takes exactly 2 arguments (1 given) 
[Finished in 0.1s with exit code 1]  

为什么会出现以上错误?

请您参考如下方法:

函数正在返回元组,因为return 只返回一项。您可以通过在元组前面加上星号来“解压缩”返回的元组。语法将如下所示:

print function1(*function2(1,2))