We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

How can I pass optional or keyword parameters from one function to another?

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)

If you have to support Python versions older than 2.0, use apply:

def f(x, *args, **kwargs):
        ...
        kwargs['width']='14.3c'
        ...
        apply(g, (x,) + args, kwargs)

CATEGORY: programming