1, the decorative device
An adorner is essentially a Python function that allows other functions to add extra functionality without any code changes, and the return value of the adorner is also a function object. It is often used for scenarios where there are aspect requirements, such as inserting logs, performance tests, transaction processing, caching, authorization validation, and so on, adorners are a great design for solving such problems, and with adorners, we can pull out a lot of similar code that is unrelated to the function itself and continue to reuse it. In summary, the function of an adorner is to add additional functionality to an already existing object.
The implementation principle of the adorner:
#parameterless Adorner principle:defLogin (funct):Print('passed user veriftcation ...') returnFunct#function returns the value or memory address of a parameterdefTV ():Print('welcom to TV page') TVV= LOGIN (TV)#executes the login function, and the parameter TV returns the memory address of the TV functionTVV ()#the Execute Generator function executes the TV function
The principle of parameter-passing decorator:
#Example: Implementing validation before executing a programdefLogin (FUNCT):#validation Functions defInner (ARG):#nested functions are designed to not allow validation functions to execute Print('passed user veriftcation ...') funct (ARG)returnInner#returns the inline function memory address to login@login#@ is the adorner's syntactic sugar, which is actually equal to Tv=login (TV)defTV (name):#Executing program functions Print('welcom%s to TV page'%name)#TV = login (TV) #相当与 @login, as the adorner doesTv'Zhang')#before executing the program, verify through the adorner#The above program execution principle explanation:#after the program executes, the adorner (@login) is first scanned inside the program after it is executed, jump to the adorner function#read the function to memory, login will return the memory address of the inner function, execute the TV, first execute the inner function, the TV parameter ' Zhang ' is passed to inner, printout, funct parameter value is TV, execute Funct will execute the TV function. Understanding the adorner principle is to wrap up the TV function and execute it before verifying it.
function return value under adorner:
defLogin (funct):defInner (*args,**Kwargs):Print('passed user veriftcation ...') returnFunct (*args,**kwargs)#The return value of the inner is the return value of the TV returnInner#@logindefTV (*args,**Kwargs):Print('welcom%s to TV page%s'%(Args,kwargs))return88#return valueTV =Login (TV) dic= {'K1':'v1','K2':'v2'}li= ['python','Java']t=TV (Dic,li)Print(t)
Adorner with pass parameter function, multilayer adorner:
#validation FunctionsdefLogin (*args,**Kwargs):Print('%s Login Verification%s! '%(Args,kwargs))#operation after landingdefQuanxian (*args,**Kwargs):Print('%s after login operation! %s'%(Args,kwargs))#Decorative DevicedefDecorator (Login_func,quanxian_func):#2 Functions of parameters defInner (Index_func):#parameter Login function defInner2 (*args,**kwargs):#receive the parameter of indexLogin_func (*args,**kwargs)#Execute the Login functionIndex_func (*args,**kwargs)#Execute the INDEX functionQuanxian_func (*args,**kwargs)#Execute the Quanxian function returnInner2#return function Address returnInner#return function Address@decorator (Login,quanxian)#performing adornersdefIndex (*args,**kwargs):#Login Function Print('Login Backstage! %s,%s'%(Args,kwargs)) L1= [1,2,3,4]dic= {'K1':'v1','K2':'v2'}index (l1,dic)#Execute function
Python3 Decorative Device