如何将列表元素作为参数传递

Ksa*_*neK 3 python

我想将list的元素作为函数参数传递,如下所示:

def foo(a,b,c):
    #do something
list=[1,2,3]
foo(list)
Run Code Online (Sandbox Code Playgroud)

我不能使用foo(list [0],list [1],list [2]),因为我不知道有多少元素列表有多少参数函数需要

看看这段代码中的输入函数:http://pastebin.com/ajDrFmzr

现在它只传递1个参数(列表),我想把它作为少数参数传递

unu*_*tbu 5

使用*参数拆包操作:

seq = [1, 2, 3]
foo(*seq)
Run Code Online (Sandbox Code Playgroud)

所以在你可以使用input功能中

getattr(self, func)(*args)
Run Code Online (Sandbox Code Playgroud)

PS.不要为变量命名list,因为它会影响同名的内置.