开发者

Python pack arguments?

开发者 https://www.devze.com 2023-01-07 16:35 出处:网络
is to possible to \"pack\" arguments in python? I have the following functions in the library, that I can\'t change (simplified):

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消