*args and **kwargs

*args collects any extra positional arguments into a tuple. **kwargs collects any extra keyword arguments into a dict. The names are a convention; the stars do the work.

The stars also work the other way, at the call site:

  • f(*nums) spreads a list or tuple into separate positional arguments.
  • f(**options) spreads a dict into keyword arguments.

So print(*[1, 2, 3]) is the same as print(1, 2, 3).

You will see def wrapper(*args, **kwargs) again in decorators, where a function must accept anything and pass it all on unchanged. That forwarding pattern is the main reason to know this well.

The order in a def is: normal parameters, *args, keyword-only parameters, **kwargs.

Run it, then call describe with a keyword it doesn't name, like color="red".