*args and **kwargs

Collecting however many arguments a caller passes, and unpacking a list or dict back into a call.

Overview

Collecting

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

total(1, 2, 3) gives args = (1, 2, 3). total() gives args = (). The function accepts any number of positional arguments without knowing in advance how many, and inside it args is an ordinary tuple.

Two stars do the same for keyword arguments:

def describe(**kwargs):

describe(name="ana", score=91) gives kwargs = {"name": "ana", "score": 91} — an ordinary dict.

The names are pure convention. *items and **options work identically; the stars carry the meaning. Convention is strong enough here that using different names in a general-purpose helper will raise eyebrows, but in a specific function a descriptive name often reads better.

args_kwargs.py

unpacking_calls.py

Worth knowing

*args collects extra positional arguments into a tuple; **kwargs collects extra keyword ones into a dict.
The names are convention, not syntax. The stars are the syntax.
Order in a definition: normal, then *args, then **kwargs.
At a call site the stars do the reverse - they spread a list or dict back into arguments.

Check yourself

0 of …

Answer without scrolling back up.

  1. Inside `def f(*args)`, what type is `args`?

  2. `volume(*[2, 3, 4])` is the same as what?

  3. Why does the wrapper pattern use both `*args` and `**kwargs`?

Cheat sheet

*args and **kwargs

One star collects any number of positional arguments into a tuple; two stars collect keyword arguments into a dictionary. At a call site the same stars run in reverse, spreading a collection back out into arguments.

PYTHON · AI.Cruz.in/python/args_and_kwargs.html

About the author

AI.Cruz builds and maintains AI.Cruz. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.