2

是否可以在 python 中“打包”参数?我在库中有以下功能,我无法更改(简化):

def g(a,b=2):
    print a,b

def f(arg):
    g(arg)

我可以

o={'a':10,'b':20}
g(**o)
10 20

但我可以/我如何通过这个f

这就是我不想要的:

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
4

1 回答 1

2

f必须接受任意(位置和)关键字参数:

def f(*args, **kwargs):
    g(*args, **kwargs)

如果您不想f接受位置参数,请忽略该*args部分。

于 2010-07-19T08:27:40.937 回答