1. 程式人生 > >python中函數的不定長參數

python中函數的不定長參數

nal 傳遞 module file rec back traceback 不定 pri

#定義一個含有不定長參數的函數,本例第三個參數*args
def sum_nums(a,b,*args):
    print(‘_‘*30)
    print(a)
    print(b)
    print(args)

#調用函數:
sum_nums(11,22,33,44,55,66,77)
sum_nums(11,22,33)
sum_nums(11,22)
sum_nums(11)#錯誤調用,傳遞的參數不夠

#輸出結果:
______________________________
11
22
(33, 44, 55, 66, 77)
______________________________
11
22
(33,)
______________________________
11
22
()
Traceback (most recent call last):
  File "6.py", line 10, in <module>
    sum_nums(11)#錯誤調用
TypeError: sum_nums() missing 1 required positional argument: ‘b‘

python中函數的不定長參數