L3·Functions & Modules

✨*args & **kwargs

Variable-length positional and keyword arguments.

*args**kwargsunpacking

§ 1Star params

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

§ 2Unpacking at call site

f(*iter, **mapping) spreads them into arguments.

§ 3Keyword-only

def f(a, *, b): b must be passed by keyword.

Example

1def show(*args, **kwargs):​
2 print("pos:", args)​
3 print("kw:", kwargs)​
4​
5show(1, 2, 3, name="Ada", age=36)​
6​
7nums = [1, 2, 3]​
8show(*nums, mode="debug")​

Try it live · Python runs in your browser

Loading playground…

Checkpoint quiz

Question 1 / 2
*args is a: