Call a function with argument list in python

To expand a little on the other answers:

In the line:

def wrapper(func, *args):

The * next to args means “take the rest of the parameters given and put them in a list called args“.

In the line:

    func(*args)

The * next to args here means “take this list called args and ‘unwrap’ it into the rest of the parameters.

So you can do the following:

def wrapper1(func, *args): # with star
    func(*args)

def wrapper2(func, args): # without star
    func(*args)

def func2(x, y, z):
    print x+y+z

wrapper1(func2, 1, 2, 3)
wrapper2(func2, [1, 2, 3])

In wrapper2, the list is passed explicitly, but in both wrappers args contains the list [1,2,3].

Leave a Comment