Forwarding keyword arguments

Ben Finney ben+python at benfinney.id.au
Tue Jun 23 20:57:18 EDT 2009


Robert Dailey <rcdailey at gmail.com> writes:

> Suppose I have 2 functions like so:
> 
> def Function2( **extra ):
>    # do stuff
> 
> def Function1( **extra ):
>    Function2( extra )

(Style note: The Python style guide, PEP 8, would have the above code
written as::

    def function2(**extra):
        # do stuff

    def function1(**extra):
        function2(extra)

See <URL:http://www.python.org/dev/peps/pep-0008> for a sensible style
guide for Python code.)

> As you can see, I would like to forward the additional keyword
> arguments in variable 'extra' to Function2 from Function1. How can I
> do this? I'm using Python 3.0.1

The syntax for “collect remaining positional arguments into a sequence”
in a function definition versus “unpack the values from this sequence
into positional arguments” in a function call are intentionally
similar::

    def bar(spam, eggs, beans):
        pass

    def foo(*args):
        bar(*args)

Likewise for the syntax for “collect remaining keyword arguments into a
mapping” in a function definition versus “unpack the items from this
mapping into keyword arguments” in a function call::

    def function2(spam, eggs, beans):
        pass

    def function1(**kwargs):
        function2(**kwargs)

For more detail, see the language reference section explaining calls
<URL:http://docs.python.org/reference/expressions.html#calls>.

-- 
 \      “A society that will trade a little liberty for a little order |
  `\     will lose both, and deserve neither.” —Thomas Jefferson, in a |
_o__)                                                letter to Madison |
Ben Finney



More information about the Python-list mailing list