Python decorator principle and related operations

Source: Internet
Author: User
Tags python decorator

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.