How to pass list elements as arguments

后端 未结 1 1585
时光取名叫无心
时光取名叫无心 2021-01-12 14:24

I want to pass elements of list as function arguments like this:

def foo(a,b,c):
    #do something
list=[1,2,3]
foo(list)

I can\'t use foo(

相关标签:
1条回答
  • 2021-01-12 14:36

    Use the * argument unpacking operator:

    seq = [1, 2, 3]
    foo(*seq)
    

    So in the input function you could use

    getattr(self, func)(*args)
    

    PS. Don't name your variables list, since it shadows the built-in of the same name.

    0 讨论(0)
提交回复
热议问题