【发布时间】:2011-12-06 10:52:43
【问题描述】:
是否有语法允许您将列表扩展为函数调用的参数?
例子:
# Trivial example function, not meant to do anything useful.
def foo(x,y,z):
return "%d, %d, %d" %(x,y,z)
# List of values that I want to pass into foo.
values = [1,2,3]
# I want to do something like this, and get the result "1, 2, 3":
foo( values.howDoYouExpandMe() )
【问题讨论】:
-
为什么不给函数一个元组?
-
@lc2817 我正在使用无法更改的库函数,并且作为参数传入的数据已经在数组中。
-
我反对将其标记为重复的决定。链接到
*args的解释是正确的,但声称这个问题重复是在要求人们玩 Jeopardy。只有知道这个问题的答案(即“使用 *args”),才能知道要搜索声称是重复的问题。