位置參數(shù)
def func(x,n):
result = x*n
默認(rèn)參數(shù)
def func(n=2):
pass
可變參數(shù)
不確定的情況下赦役,傳入list或tuple進(jìn)去
def func(*num):
pass
list = [1,2,3];
func(list)
關(guān)鍵字參數(shù)
允許你傳入0個(gè)或多個(gè)的參數(shù)烈拒,自動(dòng)組裝成tuple(元組)段磨。
def person(name, age, **kw):
print("name:", name, ", age: ",age, "other:", kw);
person('Bob', 35, city='Beijing')
也可以傳入一個(gè)dict進(jìn)去
extra = ['city':'Beijing','job':'Engineer']
person('Jane',33,**extra)
命名關(guān)鍵字參數(shù)
在中間加個(gè)*分隔符
如果中間本身有一個(gè) *args可變函數(shù),就不用加 *分隔符了
def person(name, age, *, city, job):
print(name, age, city, job)