Python theorytheory 0/50 · 0%
Functions · easy

13. *args and **kwargs

Variadic positional and keyword parameters.

`*args` collects extra positional arguments into a tuple; `**kwargs` collects extra keyword arguments into a dict.

python
def total_supply(*amounts):
    return sum(amounts)

def make_tx(**fields):
    return fields

total_supply(1, 2, 3)              # 6
make_tx(to="0xabc", value=5)       # {"to": "0xabc", "value": 5}

The same syntax unpacks arguments at a call site: `total_supply(*[1, 2, 3])` or `make_tx(**{"to": "0xabc"})`.

Check your understanding

  1. 1. What type does `*args` bundle extra positional arguments into?

  2. 2. What type does `**kwargs` bundle extra keyword arguments into?

  3. 3. What does `f(*[1,2,3])` do at a call site?