在Python中转发kwargs

5
我希望将一个函数的kwargs参数转发给另一个函数,但是要添加另一个参数。然而以下代码不起作用...
def A(**kwargs):
    B(type="moose", kwargs=kwargs)

def B(**kwargs):
    print(kwargs) # I'd like to see {"call": True, "seven": 8, "type": "moose" }

A(call=True, seven=8)
1个回答

2

只需使用**kwargs调用B()

def A(**kwargs):
    B(type="moose", **kwargs)

def B(**kwargs):
    print(kwargs) 

A(call=True, seven=8)

输出:

{'type': 'moose', 'call': True, 'seven': 8}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接