compare approaches one problem · two ways
Compare Approaches
The same problem solved different ways in Python, run on the same input, with what really differs and when to use each.
Explicit arguments vs * and ** unpacking
Call a function with values that arrive as a list and a dict.
what differs
*row spreads a sequence into positional arguments; **options spreads a mapping into keyword arguments. The function sees exactly the same call.
at a glance
| approach | use it when |
|---|---|
| A · explicit | The arguments are known and few. |
| B · * and ** | Forwarding arguments, or calling with data whose shape matches the parameters. |
Watch out: Unexpected keys in the dict raise TypeError (unexpected keyword argument), which is a useful check on untrusted shapes.
the outputs
Each approach runs on the same input.
concepts
argument unpacking*args**kwargs
learn it
related comparisons