def test(a=1,b=2):
print(a+b)
test(**dict(a=1,b=2,c=10))
输出:无法识别“c"
如图所示:
TypeError Traceback (most recent call last)
<ipython-input-177-a902992d9382> in <module>()
----> 1 test(**dict(a=1,b=2,c=10))
TypeError: test() got an unexpected keyword argument 'c'
本质上这种方式会变成 a=1,b=2,c=10进行传递;所以无法识别;
即等价于test(a=1,b=2,c=10)
结论:如果函数接收参数的方式是如此定义”a=1,b=2,c=0,……“,就不能使用**dict()方式传递非必要的参数;
根本的原因在于:受限于另外一种参数传递方式的影响;def test(kk) kk为字典;传递时test(other_dict)