Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing on named variable arguments in python

Say I have the following methods:

def methodA(arg, **kwargs):
    pass

def methodB(arg, *args, **kwargs):
    pass

In methodA I wish to call methodB, passing on the kwargs. However, it seems that if I define methodA as follows, the second argument will be passed on as positional rather than named variable arguments.

def methodA(arg, **kwargs):
    methodB("argvalue", kwargs)

How do I make sure that the **kwargs in methodA gets passed as **kwargs to methodB?

like image 666
Staale Avatar asked Sep 09 '08 08:09

Staale


1 Answers

Put the asterisks before the kwargs variable. This makes Python pass the variable (which is assumed to be a dictionary) as keyword arguments.

methodB("argvalue", **kwargs)
like image 121
Cristian Avatar answered Oct 14 '22 15:10

Cristian