參數(shù)傳遞
1.位置傳遞
必須以在被調(diào)用函數(shù)中定義的順序來傳遞
>>>def f(a,b,c):
... print a, b, c
>>>f(1, 2, 3)
1 2 3
f(1, 2, 3)中的1, 2, 3分別對應(yīng)f(a, b, c)中的a, c, c,以位置來傳遞參數(shù)
2.關(guān)鍵字傳遞
關(guān)鍵字傳遞是根據(jù)每個參數(shù)的名字傳遞參數(shù)
關(guān)鍵字傳遞參數(shù)可以不遵守位置對應(yīng)關(guān)系
關(guān)鍵字傳遞可以和位置傳遞混用。== 但位置參數(shù)要出現(xiàn)在關(guān)鍵字參數(shù)之前
==
>>>def f(a,b,c):
... print a, b, c
>>>f(1, c = 3, b = 2)
1 2 3
3.參數(shù)默認(rèn)值
定義函數(shù)時,默認(rèn)為函數(shù)的參數(shù)賦值
>>>def f(a = 10, b = 20, c = 30):
... print a, b, c
>>>f()
10 20 30
>>>f(1, b = 5)
1 5 30
所有的參數(shù)要在默認(rèn)值之前
>>> def test(a=1, b):
print a, b
SyntaxError: non-default argument follows default argument
>設(shè)置默認(rèn)參數(shù)后,當(dāng)調(diào)用函數(shù)沒有賦值時,使用默認(rèn)值;當(dāng)調(diào)用函數(shù)使用參數(shù)時,使用參數(shù)
4.包裹傳遞
在定義函數(shù)時,我們有時候并不知道調(diào)用的時候會傳遞多少個參數(shù)。這時候,包裹(packing)位置參數(shù),或者包裹關(guān)鍵字參數(shù),來進(jìn)行參數(shù)傳遞,會非常有用。
在func的參數(shù)表中,所有的參數(shù)被name收集,根據(jù)位置合并成一個元組(tuple),這就是包裹位置傳遞,為了提醒Python參數(shù),name是包裹位置傳遞所用的元組名,在定義func時,在name前加*號
>>>def func(*name):
... print type(name)
... print name
>>>func(1,4,6)
<type 'tuple'>
(1, 4, 6)
>>>func(5,6,7,1,2,3)
<type 'tuple'>
(5, 6, 7, 1, 2, 3)
==這樣傳遞的參數(shù)元組必須在位置和默認(rèn)參數(shù)后==
>>> def test(a, b=2, *c, **d):
print a, b, c,d
>>> test(1, 3, *[4, 5], **{'e':6, 'f':7})
1 3 (4, 5) {'e': 6, 'f': 7}
dict是一個字典,收集所有的關(guān)鍵字,傳遞給函數(shù)func。為了提醒Python,參數(shù)dict是包裹關(guān)鍵字傳遞所用的字典,在dict前加 **
>>>def func(**dict):
... print type(dict)
... print dict
>>>func(a=1,b=9)
<type 'dict'>
{'a': 1, 'b': 9}
>>>func(m=2,n=1,c=11)
<type 'dict'>
{'c': 11, 'm': 2, 'n': 1}
5.解包裹
在函數(shù)中,*和 **,也可以在調(diào)用的時候使用,即解包裹(unpacking)
所謂的解包裹,就是在傳遞tuple時,讓tuple的每一個元素對應(yīng)一個位置參數(shù)
>>>def func(a,b,c):
... print a,b,c
>>>args = (1,3,4)
>>>func(*args)
1 3 4
>>> def test(a, b=2, *c, **d):
print a, b, c,d
錯誤調(diào)用
>>> test(1, 3, [4, 5], {'e':6, 'f':7})
1 3 ([4, 5], {'e': 6, 'f': 7}) {}
正確調(diào)用
>>> test(1, 3, *[4, 5], **{'e':6, 'f':7})
1 3 (4, 5) {'e': 6, 'f': 7}
綜上:先位置,再關(guān)鍵字,再包裹位置(list),再包裹關(guān)鍵字(dict)
測試代碼:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
def test_it(func, *nkwargs, **kwargs):
try:
retval = func(*nkwargs, **kwargs)
result = (True, retval)
except Exception, diag:
result = (False, str(diag))
return result
def test():
funcs = (int, long, float)
vals = (1234, 12.23, '1234', '12.34')
for each_func in funcs:
print '-' * 20
for each_val in vals:
retval = test_it(each_func, each_val)
if retval[0]:
print '%s(%s) = ' % (each_func.__name__, 'each_val'), retval[1]
else:
print '%s(%s) = FAILED' % (each_func.__name__, 'each_val'), retval[1]
if __name__ == '__main__':
test()
Output:
--------------------
int(each_val) = 1234
int(each_val) = 12
int(each_val) = 1234
int(each_val) = FAILED invalid literal for int() with base 10: '12.34'
--------------------
long(each_val) = 1234
long(each_val) = 12
long(each_val) = 1234
long(each_val) = FAILED invalid literal for long() with base 10: '12.34'
--------------------
float(each_val) = 1234.0
float(each_val) = 12.23
float(each_val) = 1234.0
float(each_val) = 12.34