• python多个装饰器的执行顺序


    def decorator_a(func):
        print 'Get in decorator_a'
        def inner_a(*args, **kwargs):
            print 'Get in inner_a'
            return func(*args, **kwargs)
        return inner_a
     
    def decorator_b(func):
        print 'Get in decorator_b'
        def inner_b(*args, **kwargs):
            print 'Get in inner_b'
            return func(*args, **kwargs)
        return inner_b
     
    @decorator_b
    @decorator_a
    def f(x):
        print 'Get in f'
        return x * 2
     
    f(1)
    

      

    执行如上所示代码,最后的执行结果为:

    Get in decorator_a
    Get in decorator_b
    Get in inner_b
    Get in inner_a
    Get in f

    我们来分析下,为什么会是这样的顺序(验证的过程中可以通过打断点的方式来观察函数的程序的执行流程)。

    首先:

    1、装饰器函数在被装饰函数定义好后立即执行。

    我们把代码最后一行注掉:

    # f(1)
    重新执行,会发现最后执行结果为:

    Get in decorator_a
    Get in decorator_b
     

    说明装饰器函数在被装饰函数定义好后就立即执行。而且执行顺序是由下到上开始装饰。调用decorator_a时,f被装饰成inner_a,调用decorator_b时,f被装饰成inner_b。

    通过在最后执行:print(f), 执行结果为<function inner_b at 0x00000000038F15F8>可验证。

    所以如上所示代码中,最后执行f(1)时,f已经变成了inner_b,而inner_b中return的func,实则为inner_a, inner_a中return的func才是最终的f。

    所以最后的调用顺序为

    inner_b --->inner_a--->f

    执行结果为:

    Get in inner_b
    Get in inner_a
    Get in f

    在实际应用的场景中,当我们采用上面的方式写了两个装饰方法比如先验证有没有登录 @login_required , 再验证权限够不够时 @permision_allowed 时,我们采用下面的顺序来装饰函数:

    @login_required
    @permision_allowed
    def f()
      # Do something
      return
    

      

    总结一下:

    多个装饰器装饰函数时,有个规律是从小到上包裹(装饰)函数,从上到下执行。


    原文:https://blog.csdn.net/qq_35462323/article/details/90633796

  • 相关阅读:
    框架比较
    框架整理
    bootstrap-table中get请求携带的参数
    0514任务思路
    两台电脑对码云上面的项目进行迭代
    项目问题
    vue 中发送axios请求,解决跨域问题(没有config文件)
    正则表达式(未完待续)
    【转载】深入理解Linux文件系统
    浅谈IO优化
  • 原文地址:https://www.cnblogs.com/qbdj/p/10974631.html
Copyright © 2020-2023  润新知