Python - 函數參數需要注意的幾點

1 關鍵字參數

所用情形:參數太多時,防止參數順序對函數的影響,傳參時指定參數對應的形參名

>>> def test(brand,slogan):

               print(brand + "'s slogan is " + slogan)

>>>test(brand='Nike',slogan='just do it')

Nike's slogan is just do it

2 默認參數

>>> def test(brand='Nike',slogan='just do it'):

print(brand + "'s slogan is " + slogan)

>>> test()

Nike's slogan is just do it

3 可變參數 參數前加 *

>>> def test(*params):

print('參數的長度是:',len(params) )

>>> test(1,2,3,4)   #實現形式參數打包成元組

參數的長度是: 4


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章