Last Updated: February 25, 2016
·
6.336K
· bt3gl

Python: *args vs. **kwargs

Arguments can be defined as positional:

def func1(*args):
    return args


>>> func1(1, 2, 3)   
(1, 2, 3)

Or as * keyword*:

def func2(**kwargs):
    return kwargs

>>> func2(one=1, two=2, three=3)
{'three': 3, 'two': 2, 'one': 1}

You can also do both at once:

def func3(*args, **kwargs):
    print(args)
    print(kwargs)

>>> func3(1, 2, a=3, b=4) 
    (1, 2)
    {"a": 3, "b": 4}