可變參數(shù)指傳入的參數(shù)個數(shù)是可變的,可以是0個,1個翻具,2個...... 通過一個簡單加法例子來了解一下
def sum(*nums):
type(nums)
ret = 0
for num in nums:
ret += num
print(ret)
sum(1, 2, 3, 4)
10
通過上面的例子我們可以看出悼嫉,可變參數(shù)傳入到函數(shù)內(nèi)部時就變成了一個元組艇潭。當然直接傳入列表也是可以實現(xiàn)可變參數(shù)的效果
def sum_list(num_list):
ret = 0
for num in num_list:
ret += num
print(ret)
num_list = [1, 2, 3, 4, 5]
sum_list(num_list)
15
在單獨使用而不是在函數(shù)參數(shù)定義中使用*
會達到解包的效果,比如將一個列表傳給可變參數(shù)
sum(*num_list)
15
關鍵字參數(shù)允許向函數(shù)傳入個數(shù)可變的帶有參數(shù)名的參數(shù),可以是0個蹋凝,1個鲁纠,2個...... 通過一個例子了解一下
def student(stu_name, stu_age, stu_id, **kw):
print('stu_name:', stu_name, 'stu_age:', stu_age, 'stu_id:', stu_id, 'other:', kw)
student('Tony', '22', '20180722', hometown='New York')
stu_name: Tony stu_age: 22 stu_id: 20180722 other: {'hometown': 'New York'}
函數(shù)內(nèi)部,kw
已經(jīng)變成了一個dict
類型了鳍寂。同樣改含,我們也可以給關鍵字參數(shù)傳入一個dict
,但是同樣需要解包迄汛,但是**kw
是拷貝到函數(shù)內(nèi)部捍壤,在函數(shù)內(nèi)部更改不會影響原先的dict
tony = {'hometown':'New York', 'phone':'11111111111'}
student('Tony', '22', '20180722', **tony)
stu_name: Tony stu_age: 22 stu_id: 20180722 other: {'hometown': 'New York', 'phone': '11111111111'}
當然,可以通過控制關鍵字參數(shù)的命名來限制調(diào)用者傳入的參數(shù)鞍爱,比如我們限制student()
允許調(diào)用者傳入hometown
和phone
參數(shù)鹃觉,但不允許其他的
def student(stu_name, stu_age, stu_id, *, hometown, phone):
print(stu_name, stu_age, stu_id, hometown, phone)
student('Tony', '22', '20180722', hometown='New York', phone='11111111111')
#student('Tony', '22', '20180722', 'New York', '11111111111') #報錯
Tony 22 20180722 New York 11111111111
在定義命名的關鍵字參數(shù)時,需要使用*
將命名的關鍵字參數(shù)和位置參數(shù)分開睹逃。命名的關鍵字參數(shù)在調(diào)用時必須也把參數(shù)名也寫上盗扇,不然會報錯TypeError: student() takes 3 positional arguments but 5 were given
,意思也就是解釋器把你傳入的5個參數(shù)全部當成位置參數(shù)了沉填,而我們的函數(shù)定義時只有3個位置參數(shù)疗隶,后面的兩個是命名的關鍵字參數(shù)
python中定義函數(shù)時,有必須參數(shù)翼闹,默認參數(shù)斑鼻,可變參數(shù),關鍵字參數(shù)和命名關鍵字參數(shù)猎荠,5種參數(shù)除了可變參數(shù)和命名關鍵字參數(shù)外坚弱,其它的可以組合使用,但是要按照以上順序定義參數(shù)
def student(stu_name, stu_age=20, *, stu_id, **kw):
print('stu_name:', stu_name, 'stu_age:', stu_age, 'stu_id:', stu_id, 'kw:', kw)
student('Tony', stu_id='20170722', hometown='New York', phone='11111111111')
student('Tony', 23, stu_id='20170722', hometown='New York', phone='11111111111')
kw = {'hometown': 'A City', 'phone':'22222222222'}
student('Tony', 24, stu_id='20170722', **kw)
def student(stu_name, stu_age=20, *args, **kw):
print('stu_name:', stu_name, 'stu_age:', stu_age, 'args:', args, 'kw:', kw)
student('Tony', 'a', 'b', 'c', hometown='New York', phone='33333333333')
student('Tony', 25, 'a', 'b', 'c', hometown='New York', phone='33333333333')
args = ('a', 'b', 'c')
kw = {'hometown':'New York', 'phone':'44444444444'}
student('Tony', *args, **kw)
student('Tony', 26, *args, **kw)
stu_name: Tony stu_age: 20 stu_id: 20170722 kw: {'hometown': 'New York', 'phone': '11111111111'}
stu_name: Tony stu_age: 23 stu_id: 20170722 kw: {'hometown': 'New York', 'phone': '11111111111'}
stu_name: Tony stu_age: 24 stu_id: 20170722 kw: {'hometown': 'A City', 'phone': '22222222222'}
stu_name: Tony stu_age: a args: ('b', 'c') kw: {'hometown': 'New York', 'phone': '33333333333'}
stu_name: Tony stu_age: 25 args: ('a', 'b', 'c') kw: {'hometown': 'New York', 'phone': '33333333333'}
stu_name: Tony stu_age: a args: ('b', 'c') kw: {'hometown': 'New York', 'phone': '44444444444'}
stu_name: Tony stu_age: 26 args: ('a', 'b', 'c') kw: {'hometown': 'New York', 'phone': '44444444444'}