12

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(list[0], list[1], list[2]), because I don't know how many elements the list has and how many arguments the function takes.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
KsaneK
  • 125
  • 1
  • 1
  • 5

1 Answers1

18

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.

Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677