Adorners are essentially a function, more intuitively, the adorner is equal to the higher order function + function nesting + closure, the adorner is a function with some basic function, this function can be added to other functions, so that the function of other functions more powerful. In addition, the adorner has two important characteristics:1, does not change the function of the call Method 2, does not change the modified function of the source code , that is, the adorner is in these two features to transform the function of the other functions.
A higher order function is a function in which the function receives a function or the return value of a function is a higher order function.
Function nesting is also a function inside the function, you can nest multiple layers.
A function closure refers to a function inside a function that uses a local variable of an external function.
#Outer wrapper for receiving the modified functiondefWrapper (fun):#an inner layer function that adds basic functionality to the modified function defInner (*args,**Kwargs):#Basic Function Code #call the decorated function and receive the return valueresult = Fun (*args,**Kwargs)#returns the received result returnresult#equivalent to externally exposed inner layer functions returnInner@wrapper#equivalent to: foo = wrapper (foo)defFoo (a,b,c=3):Print('Foo', A,b,c)
@wrapperdefBar ():Print('Bar')#Called directly, without changing the way the modified function is calledFoo () bar ()
If the wrapper also needs to pass parameters, you can continue to set a layer of functions outside the wrapper
Python's path to learning-the adorner of Python basics