def w1(func): print('装饰器1....') def w1_in(): print('w1_in.....') func() return w1_in def w2(func): print('装饰器2.....') def w2_in(): print('w2_in......') func() return w2_in @w1 @w2 def f1(): print('f1....') f1()
你知道上面的代码执行的步骤吗?.................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
装饰器2.....
装饰器1....
w1_in.....
w2_in......
f1....
装饰带有带有参数的函数:
def w1(func): print('装饰器1....') def w1_in(a,b): print('w1_in.....') func(a,b) return w1_in def w2(func): print('装饰器2.....') def w2_in(a,b): print('w2_in......') func(a,b) return w2_in @w1 @w2 def f1(a,b): print('a=%d,b=%d'%(a,b)) f1(22,10)
带有参数的装饰器:
def functest(str): def func_1(func): print(str) def func_in(): print('装饰器') func() return func_in return func_1 @functest('带参数的装饰器') def f(): print('f函数') f()
带参数的装饰器
装饰器
f函数