Creating Python functions
def func2(): print('haha') # Return value of function # Return value of function,No return defined None, # There is a return value that returns this object(You can return a function object), # More than one returns a tuple return 0
Calls to Python functions
func2()
Arguments to Python functions
def func3(x,y,z=5,*args,**kwargs): print(x) print(y) print(z) func3(1,2,3) #Actual parameters(Real reference):1,2,,3 #Formal parameter(Formal parameter):x,y,z #Default parameters:z=5,(Must not be passed)
def func3(x,y,z=5,*args,**kwargs): print(x) print(y) print(z) #Transfer of position parameters,Location characteristics:Need one-to-one correspondence func3(1,2,3) #Key parameter passing,Properties of keywords:No one-to-one correspondence func3(x=1,y=2,z=3) #Unfixed parameter:*args,**kwargs #*args Receive the value of redundant position parameter,In tuple form #**kwargs Receive value of extra key parameter,In a dictionary way #Transfer of mixed parameters,Characteristic:Position parameter should precede key parameter,Inherited location and keyword properties func3(1,y=2,z=3)
Python global and local variables
def test(): name='xiaoming'#This is a local variable global name #Define a global variable
Recursion of Python functions
Three characteristics of recursion: 1. There must be a clear ending condition; 2. The scale of each problem should be reduced; 3. The efficiency of recursion is not high
def calc(n): print(n) if n>0: return calc(int(n/2)) calc(10)
Python higher order functions
There are two kinds of higher-order functions (functions or variables): 1. When a function passes a parameter to another function, return returns the memory address of a function
#Function when parameter transfer def add(a,b,i): res=i(a)+i(b) print(res) add(1.222,2.111,int) #Function when the return value returns def func4(): print('this is func4') return func5 def func5(): print('this is func5') func5=func4() func5()
Nesting of Python functions
#Nesting of functions def func6(): print('this is func6') def func7(): print('this is func7') func7() func6()
Python decorator
Condition of decorator: nesting of higher order function + function
def logger(funcTest): #Pass the memory address of the source code to the decorator def waps(*args,**kwargs): #Package interior finisher print('befor') #Contents of decoration res=funcTest(*args,**kwargs)#Call source print('after') return res #Return funcTest Results of return waps #Return the encapsulated decorator @logger #funcTest=logger(funcTest) #Source code of decorated function def funcTest(x): print('this is test file',x) return x #funcTest=logger(funcTest) #The logger returns the memory address of waps, x=funcTest(x=1) #The actual call is from the top waps,Through internal waps Called funcTest print(x)
Python three tier decorator
def auth(auth_type): #Add decorator parameters to the third layer def out(func): def wrapper(*args,**kwargs): if auth_type=='localhost': if local_login(): print('Login successful') return func(*args,**kwargs) else: print('Login failed') elif auth_type=='file': if file_login(): pass #File interface login else: print('There is no login interface') return wrapper return out @auth(auth_type='localhost') def index(): print('welcome to index page') index()