python装饰器的三层嵌套_请教Python装饰器@嵌套顺序的问题

请教大家两个Python装饰器@的问题。

问题一:嵌套。

1 def Decorator2(plugger2):

2 plugger2()

3 print ("Plugger2 内调 here!")

4 def Decorator3(plugger3):

5 plugger3()

6 print ("Plugger3 内调 here!")

7 def DecoratorTesting():

8 @Decorator2

9 def plugger2():

10 print ("Plugger2 外调 here!")

11 @Decorator3

12 def plugger3():

13 print ("Plugger3 外调 here!")

最终运行DecoratorTesting()结果是:

Plugger2 外调 here!

Plugger3 外调 here!

Plugger3 内调 here!

Plugger2 内调 here!

我对DecoratorTesting()这个结果不太理解。当第8行@Decorator2的时候,难道不是应该立即回到第1行打印“内调”吗?当第11行@Decorator3的时候,难道不是应该立即回到第4行打印“内调”吗?为什么结果却是刚才的顺序呢?

问题二:返回。

def go_Before(request, follow_up):

print ("Go to the before yields %s." %(request*2))

def go_After(request, follow_up):

print ("Go to the after yields %s." %(follow_up*3))

5 def Filter(before_func, after_func):

6 def f1(main_func):

7 def f2(request, follow_up):

8 before_result = before_func(request, follow_up)

9 if (before_result != None): return before_result;

10 main_result = main_func(request, follow_up)

11 if (main_result != None): return main_result;

12 after_result = after_func(request, follow_up)

13 if (after_result != None): return after_result;

14 return f2

15 return f1

@Filter(go_Before, go_After)

def go_Middle(request, follow_up):

print ("Go to the middle.")

最终运行go_Middle('@', '#')结果是:

Go to the before yields @@.

Go to the middle.

Go to the after yields ###.

我对go_Middle()这个结果不太理解。第9行,应该已经彻底返回了。可是为什么还会继续执行第10行和以后的部分呢?

谢谢了先!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值