**kwargs特有的作用
生活随笔
收集整理的這篇文章主要介紹了
**kwargs特有的作用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
它還有一個特殊作用,如果傳入參數的字典存在多余的參數,則可以被拋入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
?
總結
以上是生活随笔為你收集整理的**kwargs特有的作用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: para1、para2与**kw
- 下一篇: 子元素是字典列表转成字典