在Python中,可以定義包含若干參數的函數,這里有幾種可用的形式,也可以混合使用:
1. 默認參數
最常用的一種形式是為一個或多個參數指定默認值。
>>> def ask_ok(prompt,retries=4,complaint='Yes or no Please!'): while True: ok=input(prompt) if ok in ('y','ye','yes'): return True if ok in ('n','no','nop','nope'): return False retries=retries-1 if retries<0: raise IOError('refusenik user') print(complaint)
這個函數可以通過幾種方式調用:
- 只提供強制參數
>>> ask_ok('Do you really want to quit?') Do you really want to quit?yes True
- 提供一個可選參數
>>> ask_ok('OK to overwrite the file',2) OK to overwrite the fileNo Yes or no Please! OK to overwrite the fileno False
- 提供所有的參數
>>> ask_ok('OK to overwrite the file?',2,'Come on, only yes or no!') OK to overwrite the file? test Come on, only yes or no! OK to overwrite the file?yes True
2. 關鍵字參數
函數同樣可以使用keyword=value形式通過關鍵字參數調用
>>> def parrot(voltage,state='a stiff',action='voom',type='Norwegian Blue'): print("--This parrot wouldn't", action, end=' ') print("if you put",voltage,"volts through it.") print("--Lovely plumage, the",type) print("--It's",state,"!") >>> parrot(1000) --This parrot wouldn't voom if you put 1000 volts through it. --Lovely plumage, the Norwegian Blue --It's a stiff ! >>> parrot(action="vooooom",voltage=1000000) --This parrot wouldn't vooooom if you put 1000000 volts through it. --Lovely plumage, the Norwegian Blue --It's a stiff ! >>> parrot('a thousand',state='pushing up the daisies') --This parrot wouldn't voom if you put a thousand volts through it. --Lovely plumage, the Norwegian Blue --It's pushing up the daisies !
但是以下的調用方式是錯誤的:
>>> parrot(voltage=5, 'dead') SyntaxError: non-keyword arg after keyword arg >>> parrot() Traceback (most recent call last): File "<pyshell#57>", line 1, in <module> parrot() TypeError: parrot() missing 1 required positional argument: 'voltage' >>> parrot(110, voltage=220) Traceback (most recent call last): File "<pyshell#58>", line 1, in <module> parrot(110, voltage=220) TypeError: parrot() got multiple values for argument 'voltage' >>> parrot(actor='John') Traceback (most recent call last): File "<pyshell#59>", line 1, in <module> parrot(actor='John') TypeError: parrot() got an unexpected keyword argument 'actor' >>> parrot(voltage=100,action='voom',action='voooooom') SyntaxError: keyword argument repeated
Python的函數定義中有兩種特殊的情況,即出現*,**的形式。
*用來傳遞任意個無名字參數,這些參數會以一個元組的形式訪問
**用來傳遞任意個有名字的參數,這些參數用字典來訪問
(*name必須出現在**name之前)
>>> def cheeseshop1(kind,*arguments,**keywords): print("--Do you have any",kind,"?") print("--I'm sorry, we're all out of",kind) for arg in arguments: print(arg) print("-"*40) keys=sorted(keywords.keys()) for kw in keys: print(kw,":",keywords[kw]) >>> cheeseshop1("Limbuger","It's very runny, sir.","It's really very, very runny, sir.",shopkeeper="Michael Palin",client="John",sketch="Cheese Shop Sketch") --Do you have any Limbuger ? --I'm sorry, we're all out of Limbuger It's very runny, sir. It's really very, very runny, sir. ---------------------------------------- client : John shopkeeper : Michael Palin sketch : Cheese Shop Sketch >>>
3. 可變參數列表
最常用的選擇是指明一個函數可以使用任意數目的參數調用。這些參數被包裝進一個元組,在可變數目的參數前,可以有零個或多個普通的參數
通常,這些可變的參數在形參列表的最后定義,因為他們會收集傳遞給函數的所有剩下的輸入參數。任何出現在*args參數之后的形參只能是“關鍵字參數”
>>> def contact(*args,sep='/'): return sep.join(args) >>> contact("earth","mars","venus") 'earth/mars/venus'
4. 拆分參數列表
當參數是一個列表或元組,但函數需要分開的位置參數時,就需要拆分參數
- 調用函數時使用*操作符將參數從列表或元組中拆分出來
>>> list(range(3,6)) [3, 4, 5] >>> args=[3,6] >>> list(range(*args)) [3, 4, 5] >>>
- 以此類推,字典可以使用**操作符拆分成關鍵字參數
>>> def parrot(voltage,state='a stiff',action='voom'): print("--This parrot wouldn't", action,end=' ') print("if you put",voltage,"volts through it.",end=' ') print("E's", state,"!") >>> d={"voltage":"four million","state":"bleedin' demised","action":"VOOM"} >>> parrot(**d) --This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
5. Lambda
在Python中使用lambda來創建匿名函數,而用def創建的是有名稱的。
- python lambda會創建一個函數對象,但不會把這個函數對象賦給一個標識符,而def則會把函數對象賦值給一個變量
- python lambda它只是一個表達式,而def則是一個語句
>>> def make_incrementor(n): return lambda x:x+n >>> f=make_incrementor(42) >>> f(0) 42 >>> f(2) 44
>>> g=lambda x:x*2 >>> print(g(3)) 6 >>> m=lambda x,y,z:(x-y)*z >>> print(m(3,1,2)) 4
6. 文檔字符串
關於文檔字符串內容和格式的約定:
- 第一行應該總是關於對象用途的摘要,以大寫字母開頭,並且以句號結束
- 如果文檔字符串包含多行,第二行應該是空行
>>> def my_function(): """Do nothing, but document it. No, really, it doesn't do anything. """ pass >>> print(my_function.__doc__) Do nothing, but document it. No, really, it doesn't do anything. >>>
