gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:54:59 25 4
gpt4 key购买 nike

代码:

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))   

关于python - 将函数调用作为函数参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30987651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com