开发者

Python pack arguments?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜