Computer >> Computer tutorials >  >> Programming >> Python

How to call a function with argument list in Python?


def baz1(foo, *args):

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

In the line:

foo(*args)

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

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

def baz1(foo, *args): # with star
     foo(*args)
def baz2(foo, args): # without star
    foo(*args)
def foo2(x, y, z):
    print x+y+z
baz1(foo2, 2, 3, 4)
baz2(foo2, [2, 3, 4])

OUTPUT

9
9