不函数则{无组织无结构/冗余/代码阅读性差/维护成本高}
函数分:内置/自定义
python中函数也要先定义后使用(定义阶段.....只检测语法错误,使用阶段)
参数传入也无类型限制
def 函数名(参数1,参数2,....):
'''文档注释'''
函数体
返回值
函数的三种形式:无参函数/有参函数/空函数
函数调用三种形式:语句形式/表达式方式/当做参数传入
1 def bb_max(x,y):
2 res=x if x>y else y
3 return res
4 def fee()
5 pass
6
7
8 fee()#语句形式
9 res_max = bb_max(10,50) #表达式方式
10 th_max =bb_max(bb_max(50,100),1000)#当做参数传入
函数返回值:没有返回值时实际返回:None
可返回多值 : return 值1,值2,值3... 且无返回类型限制 参数传入也无类型限制
函数返回值取值技巧:
1 def fee(a,b,c) 2 return x,y,z 3 m,n,o=fee() 4 #m=a n=b o=z
若返回值太多,而只想取某几个值可以用_或者*_代替
m,_,n=fee()
若返回值有很多,可以用m,*_,n=fee()取到第一个和最后一个
函数的参数可分为:形参和实参
详细区分可分为:位置参数/关键字参数/默认参数/可变长参数(*args,**kwargs)/命名关键字参数
def foo(x,y,z):#位置形参:必须被传值的参数 print(x,y,z) foo(1,2,3) foo(1,2,3) #位置实参数:与形参一一对应
foo(x=1,z=2,y=3)#关键字参数
关键字参数需要注意的问题:
1:关键字实参必须在位置实参后面
2: 不能重复对一个形参数传值
foo(1,z=3,y=2) #正确
foo(x=1,2,z=3) #错误
foo(1,x=1,y=2,z=3)#错误
默认参数:
def register(name,age,sex='male'): #形参:默认参数 print(name,age,sex) register('asb',age=40) register('a1sb',39) register('a2sb',30) register('a3sb',29) register('王蛋',20,'female') register('刘蛋',sex='female',age=19)#错误 关键字实参必须在位置实参后面
默认参数需要注意的问题:
一:默认参数必须跟在非默认参数后
def register(sex='male',name,age): #在定义阶段就会报错
print(name,age,sex)
(了解)二:默认参数在定义阶段就已经赋值了,而且只在定义阶段赋值一次
a=100000000
def foo(x,y=a):
print(x,y)
a=0
foo(1) # 打印结果 1 100000000
三:默认参数的值通常定义成不可变类型
可变长参数
ef foo(x,y,*args): #*会把溢出的按位置定义的实参都接收,以元组的形式赋值给args print(x,y) print(args) foo(1,2,3,4,5) 应用场景: def add(*args): res=0 for i in args: res+=i return res print(add(1,2,3,4)) print(add(1,2)) def foo(x, y, **kwargs): # **会把溢出的按关键字定义的实参都接收,以字典的形式赋值给kwargs print(x, y) print(kwargs) foo(1,2,a=1,name='egon',age=18) 应用场景: def foo(name,age,**kwargs): print(name,age) if 'sex' in kwargs: print(kwargs['sex']) if 'height' in kwargs: print(kwargs['height']) foo('egon',18,sex='male',height='185') foo('egon',18,sex='male')
命名关键字参数:
def foo(name,age,*,sex='male',height): print(name,age) print(sex) print(height) #*后定义的参数为命名关键字参数,这类参数,必须被传值,而且必须以关键字实参的形式去传值 foo('egon',17,height='185')
参数混合使用:
def foo(name,age=10,*args,sex='male',height,**kwargs): # def foo(name,age=10,*args,sex='male',height,**kwargs): # print(name) # print(age) # print(args) # print(sex) # print(height) # print(kwargs) # # foo('alex',1,2,3,4,5,sex='female',height='150',a=1,b=2,c=3) # def foo(*args): # print(args) # foo(1,2,3,4) # 1,2,3,4 <=====>*(1,2,3,4) #*['A','B','C','D'],=====>'A','B','C','D' # foo(*['A','B','C','D']) #foo('A','B','C','D') # foo(['A','B','C','D']) # # def foo(x,y,z): # print(x,y,z) # # # foo(*[1,2,3]) #foo(1,2,3) # foo(*[1,2]) #foo(1,2) # def foo(**kwargs): # print(kwargs) # # #x=1,y=2 <====>**{'y': 2, 'x': 1} # # foo(x=1,y=2) # # foo(**{'y': 2, 'x': 1,'a':1}) #foo(a=1,y=2,x=1) # def foo(x,y,z): # print(x,y,z) # # # foo(**{'z':3,'x':1,'y':2}) #foo(x=1,z=3,y=2) # foo(**{'z':3,'x':1}) #foo(x=1,z=3) # def foo(x,y,z): # print('from foo',x,y,z) # # def wrapper(*args,**kwargs): # print(args) # print(kwargs) # # # wrapper(1,2,3,a=1,b=2) # # def foo(x,y,z): # print('from foo',x,y,z) # def wrapper(*args,**kwargs): # print(args) #args=(1,2,3) # print(kwargs) #kwargs={'a':1,'b':2} # foo(*args,**kwargs) #foo(*(1,2,3),**{'a':1,'b':2}) #foo(1,2,3,b=2,a=1) # # wrapper(1,2,3,a=1,b=2) # wrapper(1,z=2,y=3) # def foo(x,y,z): # print('from foo',x,y,z) # def wrapper(*args,**kwargs): # # print(args) #args=(1,) # # print(kwargs) #kwargs={'y':3,'z':2} # foo(*args,**kwargs) #foo(*(1,),**{'y':3,'z':2}) #foo(1,z=2,y=3) # # wrapper(1,2,3,a=1,b=2) # wrapper(1,z=2,y=3) # #补充:函数定义阶段到底干了什么事情:只检测函数体的语法,并不会执行 # def bar(): # x # if 1 >2: # print('====>') # # bar()
#补充:函数定义阶段到底干了什么事情:只检测函数体的语法,并不会执行# def bar():# x# if 1 >2:# print('====>')## bar()