在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创建的是有名称的。
>>> 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. >>>