variable argument unpacking

Alexandre Paloschi Horta alexandre.horta at gmail.com
Wed Jun 1 03:59:43 EDT 2016


The way you defined the function:

def a(a = 1, b = 2, c = 3, *d, **e):
        print(a, b, c)
        print(d)
        print(e)

a, b and c are positional arguments. d will be filled with the excess arguments and e will receive a dictionary, if supplied.

One thing is the function definition, another is the function call. If you pass a number of arguments, the first three will be assigned to a, b and c no matter what, even if you supplied defaults.

Peter's solution turns a, b and c into keyword arguments. That way you can call the function with an arbitrary number of arguments and a, b and c will keep the default values, unless you be explicit about the values you want to assign to a, b and c.



More information about the Python-list mailing list