*args and **kwargs (Python)

*args lets a function accept any number of positional arguments as a tuple, and **kwargs accepts any number of keyword arguments as a dictionary.

Python

The names args and kwargs are just convention — the stars are what matter. Use them when you don't know in advance how many arguments a function will receive.

Syntax

def f(*args, **kwargs):

Examples

Python

def total(*args):
    return sum(args)

print(total(1, 2, 3, 4))

Output:

10

Python

def info(**kwargs):
    for k, v in kwargs.items():
        print(k, v)

info(name="Ed", age=9)

Output:

name Ed
age 9

Learn it in a lesson

See also

Back to the Dictionary