gpt4 book ai didi

python - 将元组扩展为参数

转载 作者:太空宇宙 更新时间:2023-11-03 20:07:52 24 4
gpt4 key购买 nike

假设我有一个类似的函数:

def myfun(a, b, c):
return (a * 2, b + c, c + b)

给定一个元组 some_tuple = (1, "foo", "bar"),我如何使用 some_tuple 调用 myfun?这应该输出结果(2, "foobar", "barfoo")

我知道可以定义 myfun 以便它直接接受元组,但我想调用现有的 myfun

<小时/>

另请参阅:What do ** (double star/asterisk) and * (star/asterisk) mean in a function call? .

最佳答案

myfun(*some_tuple) 完全符合您的要求。 * 运算符只是解压元组(或任何可迭代对象)并将它们作为位置参数传递给函数。了解更多关于 unpacking arguments .

关于python - 将元组扩展为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58872598/

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