The python adorner, simply the code used to manipulate the underlying code, validates the underlying code without changing the underlying code function, etc.
First, it must be known that the difference between the call Func and Func, respectively, is to return the memory address where the function resides and call the function, output execution results, for example:
def func (): Print (" welcome!!!" ")print(" the memory address where the function is returned:", Func) func ()
List A simple Web page invocation Example
1 #Do login Verification2 defLogin (func):3 Print("Login Successful")4 returnfunc5 6 defindex (name):7 Print("Welcome "%s" to the homepage! "@name)8 defTV (name):9 Print("Welcome to "%s" to the TV page! "@name)Ten defTV (): One Print("Welcome to the TV page! ") A -tv=Login (TV) -TV ()
From the above code, in order to perform login verification, need to do extra work, change the operation of the Department, there is no way directly to use the Department of TV () to invoke authentication?
1 EF Login (func):2 defInner (*args):3 Print("Login Successful")4Func (*args)5 returnInner6 7 @login8 defindex (name):9 Print("Welcome%s to Index"%name)Ten @login One defTV (Name,password): A Print("Welcome%s to TV"%name) - #def TV (): - #print ("Welcome%s to TV") the defmoive (name): - Print("Welcome%s to Movie"%name) - - #tv=login (TV) + #TV () -Tv"Eric","123") +Index"Eeric")
Function execution: The function executes login sequentially, then reverses to @login, passes the index information into memory, returns the inner function called by index, and proceeds to the following function, returning the inner function inner called by TV and continuing with the Moive function below. Then proceed to execute the call below, return to inner to continue executing the inner inner statement, and invoke the function execution;
The function of the adorner: to implement the operation of the underlying function without altering the internal logic of the original underlying function
Python adorner principle and related actions