Superposition decorator
When a function overlay multiple decorators, the program how to run it?
Superposition decorator:
In the same object to be decorated, add multiple decorators, and executed.
@ Decoration 1
@ 2 decoration
@ decoration 3
DEF decorated objects ():
Pass
Note: decorator calling function is only performed when added decorative objects.
Decorative sequence: from the bottom to the (wrapped)
The order of execution: down by the upper (through)
def wrapper1(func): def inner1(*args, **kwargs): print('我是inner1') res = func(*args, **kwargs) print('我是inner111111111111') return res return inner1 def wrapper2(func): def inner2(*args, **kwargs): print('我是inner2') res = func(*args, **kwargs) print('I inner2222222 ' ) return RES return Inner2 @ wrapper2 @ wrapper1 DEF func1 (): Print ( ' I am !!!!!!!!! 1 func1 ' ) func1 ()
The output is:
I inner2 I inner1 I func1! ! ! ! ! ! ! ! ! 1 I inner111111111111 I inner2222222