python可變參數(shù)的使用注意
1、函數(shù)傳入實參,可變參數(shù)(*)之前的參數(shù)不能指定參數(shù)名。
>>>defmyfun(a,*b):
...print(a
)...print(b)
...
>>>myfun(a=1,2,3,4)
File"",line1
SyntaxError:positionalargumentfollowskeywordargument
>>>myfun(1,2,3,4)
1
(2,3,4)
2、函數(shù)傳入實參,可變參數(shù)(*)之后的參數(shù)必須指定參數(shù)名,否則就會被歸到可變參數(shù)之中。
>>>defmyfun(a,*b,c=None):
...print(a)
...print(b)
...print(c)
...
>>>myfun(1,2,3,4)
1
(2,3,4)
None
>>>myfun(1,2,3,c=4)
1
(2,3)
4
以上就是python可變參數(shù)的使用注意,希望對大家有所幫助。更多Python學習教程請關注IT培訓機構:千鋒教育。