字典传参的错误理解
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)
總結
- 上一篇: 集合中元素的数据类型可以不同,但集合中不
- 下一篇: vim折叠的使用方法