Advanced knowledge is for developers of packages and class libraries. Functions are only executable code, not objects, closures and functional programming
Closure = function + environment variable
a=10 def outer(): a=25 def inner(x): print(a*x*x) return inner f=outer() # __Close? Built in variable # environment variable print(f.__closure__[0].cell_contents) #25 f(2) # 100
def f1(): a=10 def f2(): # A is regarded as a local variable by python at this time. The local variable does not affect the external variable a=20 print(a) #20 print(a) #10 f2() print(a) #10 f1() # 10 20 10
#Non closure method x=0 def f1(num): #Global keyword can use global variables global x y=x+num x=y return y print(f1(3)) #3 print(f1(5)) #5 print(f1(8)) #8 #Closure method def f1(): x=0 def f2(steps): #Nonlocal keywordforces a variable to be a nonlocal local variable nonlocal x x=x+steps return x return f2 f=f1() print(f(3)) #3 print(f(5)) #8 print(f(8)) #16
Anonymous function
#Anonymous function lambda does not need return function body but only expression f=lambda x,y:x+y print(f(2,3)) #5
Ternary expression
x> Y returns x otherwise y
if condition else condition is false
r=x if x>y else y
map
list_x=[1,2,3,4,5] def squre(x): return x*x # for v in list_x: # squre(v) r=map(squre,list_x) # [1, 4, 9, 16, 25] print(list(r))
map and lambda
list_x=[1,2,3,4,5,6] list_y=[1,2,3,4,5] # List > x list > y r=map(lambda x,y:x*x+y,list_x,list_y) #[2, 6, 12, 20, 30] print(list(r))
reduce
from functools import reduce # reduce continuous calculation list1=['1','2','3','4','5'] #reduce(lambda expression, parameter sequence, initial value) # Execution procedure (((('aaa'+'1')+'2')+'3')+'4')+'5' r=reduce(lambda x,y:x+y,list1,'aaa') print(r) #aaa12345 #map/reduce functional programming big data programming model mapping reduction parallel computing
filter
list1=[1,0,0,1,1] # Case filter list2=['a','B','C','d '] #x> 0 returns a Boolean value r=filter(lambda x:x>0,list1) #filter returns a collection print(list(r)) #[1, 1, 1]
Imperative programming and functional programming
Imperative programming: def if else for class objects, etc
Functional programming: lambda: operator map reduce filter makes code more concise
Decorator
#Decorator # Print the current time before each function import time #func: core function def decorate(func): # *arg variable parameter * * kw: Dictionary composed of key parameters def wrapper(*arg,**kw): #unix timestamp: the number of seconds since January 1, 1970 (midnight of UTC/GMT), regardless of leap seconds. print(time.time()) # A general method of writing unknown function parameters func(*arg,**kw) return wrapper #1 Parameter @ decorator name syntax sugar @decorate def f1(func_name): print("this is function1"+func_name) #2 Parameters @decorate def f2(func_name1,func_name2): print("this is function2"+func_name1) print("this is function2"+func_name2) # Case with key parameter @decorate def f3(func_name1,func_name2,**kw): print("this is function3"+func_name1) print("this is function3"+func_name2) print(kw) # {'a': 1, 'b': 2, 'c': 3} # No reference @decorate def f4(): print("this is function4") # Do not change the call mode of the original function #1538815374.121, this is function1 parameter 1 f1("Parameter 1") # 1538815374.122 this is function2 Parameter 1 this is function2 parameter2 f2("Parameter 1","Parameter 2") #1538815374.122,this is function3 parameter 1,this is function3 parameter 2,{'a': 1, 'b': 2, 'c': 3} f3("Parameter 1","Parameter 2",a=1,b=2,c=3) #1538815374.125,this is function4 f4() # Application of decorator: Flash framework @ api.router makes function a controller # There is also a decorator @ auth.login.required that can be used to set whether an external accessible interface or an interface can only be accessed by logging in