它还有一个特殊作用,如果传入参数的字典存在多余的参数,则可以被抛入kwargs中; 如下所示:
case1
def test(a=1,b=3,c=10,d=20):
print(c)
print(d)
p = dict(c=100,d=200)
test(a=1,b=3,**p)
输出 :
100
200
case2
def test(a=1,b=3,c=10,d=20):
print(c)
print(d)
p = dict(c=100,d=200,e=100)
test(a=1,b=3,**p)
TypeError Traceback (most recent call last)
<ipython-input-149-5219402a150e> in <module>
3 print(d)
4 p = dict(c=100,d=200,e=100)
----> 5 test(a=1,b=3,**p)
TypeError: test() got an unexpected keyword argument 'e'
case3
def test(a=1,b=3,c=10,d=20,**kwargs):
print(c)
print(d)
p = dict(c=100,d=200,e=100)
test(a=1,b=3,**p)
100
200