is to possible to "pack" arguments in python? I have the following functions in the library, that I can't change (simplified):
def g(a,b=2):
print a,b
def f(a开发者_JAVA百科rg):
g(arg)
I can do
o={'a':10,'b':20}
g(**o)
10 20
but can I/how do I pass this through f
?
That's what I don't want:
f(**o)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() got an unexpected keyword argument 'a'
f(o)
{'a': 10, 'b': 20} 2
f
has to accept arbitrary (positional and) keyword arguments:
def f(*args, **kwargs):
g(*args, **kwargs)
If you don't want f
to accept positional arguments, leave out the *args
part.
精彩评论