• 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
    总结一下:

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

  • 相关阅读:
    发邮件遇到 Failure sending mail.The remote name could not be resolved: 'www.youdomain.com' 问题的解决方法
    machine.config, inetinfo.exe, aspnet_wp.exe, aspnet_state.exe这些文件的作用于位置.
    IIS的变迁(IIS3, IIS4, IIS5, IIS6, IIS7)
    精简代码 (转)
    新年第一帖——元旦这天骑车迷路了
    我是月亮,也想做那天上的太阳
    记几点吧
    谈谈电影
    闺蜜
    大气
  • 原文地址:https://www.cnblogs.com/hyhy904/p/10961996.html
Copyright © 2020-2023  润新知