gpt4 book ai didi

python - 接受函数参数的返回值

转载 作者:行者123 更新时间:2023-11-28 19:45:23 24 4
gpt4 key购买 nike

所以我正在尝试使用一个返回值的函数,但我希望将这些值返回到另一个函数中。下面是我需要的示例。

def returner():
x=1
y=2
z=3
return x,y,z
def tester(arg1,arg2,arg3):
print arg1,arg2,arg3

tester(returner())

我想要它做的是打印 1,2,3,但是我无法用它来做,因为它说“测试器正好需要 3 个参数,给定 1 个。”有没有我遗漏的东西或者这是不可能的?

最佳答案

您想使用 * - splat(或星号)运算符:

tester(*returner())

这是参数解包 - 它将返回值的元组解包到函数的参数中。

>>> def test():
... return 1,2,3
...
>>> def test2(arg1, arg2, arg3):
... print(arg1, arg2, arg3)
...
>>> test2(*test())
1 2 3

关于python - 接受函数参数的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10376506/

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