A simple decorative device
1. Why use adorners
The function of the adorner: the function of the original function is extended without modifying the original function and its calling mode
The essence of adorners: a closure function
look at a simple decorator: the ability to calculate the execution time of each function
Import TimedefWrapper (func):definner (): Start=time.time () func () End=time.time ()Print(end-start)returnInnerdefhahaha (): Time.sleep (1) Print('AAAAA') hahaha=wrapper (hahaha) hahaha ()calculate the execution time of a function
2. Grammar Sugar
import time def wrapper (func): def inner (): Start =time.time () F UNC () End =time.time () Print (End-start) return inner@wrapper def KKK (): # print ( " aaaaa " ) KKK ()
Decorator-Grammar sugar
Adorner with Parameters
Import TimedefTimer (func):defInner (*args,**Kwargs): Start=time.time () re= Func (*args,**Kwargs) End=time.time ()Print(end-start)returnRereturnInner@timer#==> func1 = Timer (func1)defFunc1 (A, b):Print('In func1') Print(A, b) @timer#==> func1 = Timer (func1)defFunc2 (a):Print('In Func2 and get a:%s'%(a))return 'fun2 over'Func1 (The)Print(Func2 ('aaaaaa'))the original function adorner with multiple parameters
Adorner with return value
Import TimedefTimer (func):defInner (*args,**Kwargs): Start=time.time () re= Func (*args,**Kwargs) End=time.time ()Print(End-start)returnRereturnInner@timer#==> func1 = Timer (func1)defJJJ (a):Print('In jjj and get a:%s'%(a))return 'fun2 over'JJJ ('aaaaaa')Print(JJJ ('aaaaaa'))adorner with return value
Ii. the principle of open closure
1. Open for expansion
2. The modification is closed
Three, the fixed structure of the adorner
Import TimedefWrapper (func):#Decorative Device defInner (*args, * *Kwargs):" "content extension prior to function execution" "ret= Func (*args, * *Kwargs)" "content extension prior to function execution" " returnretreturnInner@wrapper#=====>aaa=timmer (AAA)defaaa (): Time.sleep (1) Print('FDFGDG') AAA ()Fixed Format
Four, with parameters of the adorner
Adorner with parameters: it is to pass the parameter to the adorner
Function: is when added a lot of decorations, and now suddenly do not want to add a decorator, want to remove the adorner, but so much code, a trouble to go idle, then, we can use the adorner with parameters to decorate it, which he is like a switch, when it is called, It is removed when not in use. Give a parameter to the adorner, then the grammatical sugar should also be enclosed in parentheses. The parentheses in the syntactic sugar incoming the argument. Here, we can use three layers of nesting, get a logo to go to identify.
#Adorner with parameters: (equivalent to a switch) in order to pass a parameter to the adorner#when f=true# is True, the decorator is added.F=false#when False, the decorator is removed.defouter (flag):defWrapper (func):defInner (*args,**Kwargs):ifflag:Print('before') ret=func (*args,**Kwargs)Print(' After') Else: Ret= Func (*args, * *Kwargs)returnretreturnInnerreturnWrapper@outer (F)#@wrapperdefhahaha ():Print('hahaha') @outer (F)defShuangwaiwai ():Print('Shuangwaiwai') hahaha () Shuangwaiwai ()adding parameters to the adorner
Five or more adorners decorate a function
defqqqxing (fun):defInner (*args,**Kwargs):Print('In Qqxing:before') ret= Fun (*args,**Kwargs)Print('In qqxing:after') returnretreturnInnerdefPipixia (fun):defInner (*args,**Kwargs):Print('In Qqxing:before') ret= Fun (*args,**Kwargs)Print('In qqxing:after') returnretreturnInner@qqqxing@pipixiadefDapangxie ():Print('are you hungry?') Dapangxie ()" "execution order of the @qqqxing and @pipixia: First execute the print (' in Qqxing:before ') inside the qqqxing and then jump to the print (' in Qqxing:before ') inside the Pipixia ret = Fun (*args,**kwargs) print (' in Qqxing:after '), finished and returned to the qqqxing inside the print (' in Qqxing:after '). So just like the results of the following Operation" "multiple adorners decorate a function
VI. statistics How many functions have been decorated for small applications
count How many functions I've decorated l=[]defWrapper (Fun): L.append (Fun)#statistics the number of functions in the current program is decorated defInner (*args,**Kwargs):#l.append (fun) #统计本次程序执行有多少个带装饰器的函数被调用了ret = Fun (*args,**Kwargs)returnretreturnInner@wrapperdefF1 ():Print('In F1') @wrapperdefF2 ():Print('In F2') @wrapperdeff3 ():Print('In F3')Print(l)statistics How many functions are decorated
python--Decorator