Home  >  Article  >  Backend Development  >  The calling order of multiple decorators in python

The calling order of multiple decorators in python

高洛峰
高洛峰Original
2017-02-17 11:01:191085browse

Preface

Decorator is a function often used in program development, and it is also the basic knowledge of python language development. If decorators can be used reasonably in the program, it can not only improve development efficiency, but also It can make the code you write look high-end^_^

Usage scenarios

There are many places where decorators can be used. A simple example is the following scenario

  • Introducing logs

  • Function execution time statistics

  • Preparatory processing before executing the function

  • Cleaning function after executing the 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_login
def ucOrder(request,context,pIndex):
    '''
    获取数据
    处理数据
    传递到页面上去

The above is a case of using decorator in a simple e-commerce application, in which the ucOrder function can only be executed after the user logs in. If you don't use a decorator, the common approach may be to write a bunch of verification codes in ucOrder to determine whether the user is logged in, and then determine the subsequent execution logic, which is more troublesome.

Then it is relatively simple after using the decorator. You only need to follow the format of the decorator and add @user_login to the ucOrder function. Then when the python interpreter is running, it will explain it from top to bottom. The code first executes the user_login function, and passes in ucOrder as a parameter of the user_login function, which is equivalent to user_login(ucOrder). This serves as a function to verify whether the user is logged in and decide whether to execute the ucOrder function

The calling sequence used by multiple decorators

def one(func):
    print('----1----')
    def two():
        print('----2----')
        func()
    return two

def a(func):
    print('----a----')
    def b():
        print('----b----')
        func()
    return b

@one
@a
def demo():
    print('----3----')

demo()

Execution results:

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

As you can see from the execution results, if multiple decorators are used, the execution order is still a little bit Weird, why is this happening?

Regarding this issue, there are better articles that can explain, Python decorator execution order myths


More python multiple decorator calling order For related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn