Call sequence of multiple decorator in python

Source: Internet
Author: User
Tags python decorator
Preface the decorator is a common function in program development and a basic knowledge of python language development. if you can reasonably use the decorator in the program, it not only improves the development efficiency, in addition, you can make the written code look very tall. you can use the decorator in the use case... preface

The decorator is a feature that is often used in program development. it is also the basic knowledge of python language development. if you can reasonably use the decorator in the program, it not only improves the development efficiency, it also makes the written code look very tall. ^_^

Use cases

There are many places to use the decorator. Simple examples are as follows:

  • Import logs

  • Function execution time statistics

  • Pre-processing

  • Clear function after executing function

  • Permission verification and other scenarios

  • Cache

Decorator case
def user_login(fun):    def islogin(request,*args,**kwargs):        context = {}        if request.session.has_key('uname'):            context['uname'] = request.session.get('uname')        else:            context['uname'] = 'None'        return fun(request,context,*args,**kwargs)    return islogin
@ User_logindef ucOrder (request, context, pIndex): ''' get the data to be processed and pass it to the page.

The above is a case where the decorator is used in a simple e-commerce application. The ucOrder function is executed only after you log on. If you do not use the decorator, the common practice may be to write a pile of verification code in ucOrder to determine whether the user is logged on, and then decide the execution logic behind it, which is more troublesome.

After the decorator is used, it is relatively simple. you only need to add @ user_login to the ucOrder function according to the format used by the decorator. Then, when the python interpreter is running, the code is interpreted from top to bottom. the user_login function is executed first, and the ucOrder is passed in as the parameter of the user_login function, which is equivalent to user_login (ucOrder ), in this way, you can check whether the user is logged on and decide whether to execute the ucOrder function.

Call sequence used by multiple decorator
def one(func):    print('----1----')    def two():        print('----2----')        func()    return twodef a(func):    print('----a----')    def b():        print('----b----')        func()    return b@one@adef demo():    print('----3----')demo()

Execution result:

/usr/bin/python2.7 /home/python/Desktop/tornadoProject/one.py----a--------1--------2--------b--------3----

We can see from the execution results that if multiple decorator are used, the execution sequence is still a bit weird. why?

There are better articles to explain about this issue. The execution sequence of the Python decorator is confused.


For more python-related articles on the call sequence of multiple decorators, refer to the PHP Chinese network!

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.