Zariko's Place

How to use *args and **kwargsΒΆ

*args and **kwargs are 2 useful python mechanisms to define and call methods or functions with a variable number of arguments.

*args is a list :

def test_var_args(*args):
    for arg in args:
        print("arg:", arg)

test_var_arg(172, "3", "foo", [1,2,3])

**kwargs is a dictionary-like structure :

def test_var_kwargs(**kwargs):
    for key in kwargs:
        print("arg: %s = %s" % (key, kwargs[key]))

test_var_kwargs(key1=3, key2="foo", foo=[1,2])