How can I pass optional or keyword parameters from one function to another?
PythonLevel: Intermediate
How can I pass optional or keyword parameters from one function to another?
Hints
- Identify object types, mutability and execution order.
- Trace a minimal example: which values and references change?
Answer
Collect the arguments using the * and ** specifiers in the function’s
parameter list; this gives you the positional arguments as a tuple and the
keyword arguments as a dictionary. You can then pass these arguments when
calling another function by using * and **:
def f(x, *args, **kwargs):
...
kwargs['width'] = '14.3c'
...
g(x, *args, **kwargs)
Source and licence: Python 3.14.0 Programming FAQ — Python Software Foundation · PSF-2.0