Decorator
1, open closed principle
Open: For adding new features is open
Closed: For modify the original function is closed
2, the role of decorators
Add new features to the original function without changing the original premise of function call
3, decorator
. 1 # ① primer - Why have decorator 2 in order not to modify the original basis function is a function of the new functionality is added, resulting in a decorator . 3 . 4 # ② simple decorator . 5 DEF Deco (F): . 6 DEF warpper () : 7 "" " prepro function to add features " "" . 8 F () . 9 "" " added after the original functional capabilities " "" 10 return warpper . 11 12 is DEF FUNC (): 13 is Print ( ' which is the original function! ') 14 15 functioning = deco (function) 16 func () 17 18 #Syntactic sugar ③ decorator . 19 DEF Deco (F): 20 is DEF warpper (): 21 is "" " prepro function to add features " "" 22 is F () 23 is " '" added after the original functional capabilities "" " 24 return warpper 25 26 is @deco # -> where the same effect as Deco = FUNC (FUNC) 27 DEF FUNC (): 28 Print ( ' which is the original function ' ) 29 30 FUNC () 31 is 32 # ④ return value of decorator 33 is DEF Deco (F): 34 def wrapper(): 35 "" " prepro function to add features " "" 36 RES = F () 37 [ " '" added after the original functional capabilities "" " 38 is return RES 39 return warpper 40 41 is @deco 42 is DEF FUNC (): 43 is Print ( ' which is the original function ' ) 44 is 45 FUNC () 46 is 47 # ⑤ parameters with values returned decorator 48 DEF Deco (F): 49 DEF warpper (* args, ** kwargs): 50 "" "Add the original function before the function "" " 51 F = RES (* args, ** kwargs) 52 is "" " added after the original functional capabilities " "" 53 is return RES 54 is return warpper 55 56 is @deco 57 is DEF FUNC (* args, ** kwargs): 58 Print ( ' this is the original function ' ) 59 60 FUNC (* args, ** kwargs) 61 is 62 is # ⑥ multilayer decorative unit 63 is # TODO 64 65 # ⑦ is modified with a plurality of decorative function 66 # TODO
4, the fixed format decorator
. 1 DEF Deco (F): 2 DEF warpper (* args, ** kwargs): . 3 "" " prepro function to add features " "" . 4 RES = F (* args, ** kwargs) . 5 "" " primitive function after the addition function "" " . 6 return RES . 7 return warpper . 8 . 9 @deco 10 DEF FUNC (* args, ** kwargs): . 11 Pring ( ' which is the original function ' ) 12 is 13 is FUNC (* args, ** kwargs)
5, the decorator plate fixed format -wraps
If you want to use the original method under a double function, you need to call the system decorator @ wraps (func)
. 1 from functools Import Wraps 2 . 3 DEF Deco (FUNC): . 4 @wraps (FUNC) # is applied directly above the innermost function . 5 DEF warpper (* args, ** kwargs): . 6 return FUNC (* args, ** kwargs ) . 7 return warpper . 8 . 9 @deco 10 DEF origin_func (): . 11 '' ' 12 is that the original function is a comment 13 is : return: 14 ' '' 15 Print ( ' which is the original function ' ) 16 17 # Although the implementation of the decorator, origin_func has been pointing wrapper, but then if the @wraps (func) decorator double call method origin_func is still under the original function origin_func of 18 Print (origin_func. __Name__ ) 19 >>> origin_func 20 is 21 is Print (. origin_func the __doc__ ) 22 is >>> this is the original function of the comment 23 is >>>: return :
6, with parameters decorator
. 1 DEF Outer (In Flag): 2 DEF Timer (FUNC): . 3 DEF Inner (* args, ** kwargs): . 4 IF In Flag: . 5 Print ( '' ' performs the functions previously done ' '' ) . 6 Re = FUNC (* args, ** kwargs) . 7 IF in Flag: . 8 Print ( '' ' do after the execution of the function ' '' ) . 9 return Re 10 return Inner . 11 return Timer 12 is 13 is #Here first function call outer (False) -> Returns Timer -> Timer @ -> FUNC = Timer (FUNC) -> Inner FUNC = 14 @outer (False) 15 DEF FUNC (): 16 Print (111 ) . 17 18 is func ()
7, a plurality of the same decorative function decorator
1 def wrapper1(func): 2 def inner1(): 3 print('wrapper1 ,before func') 4 func() 5 print('wrapper1 ,after func') 6 return inner1 7 8 def wrapper2(func): 9 def inner2(): 10 print('wrapper2 ,before func') 11 func() 12 print('wrapper2 ,after func' ) 13 is return Inner2 14 15 @ wrapper2 # will inner1 decorated, i.e. inner1 = wrapper2 (inner1) = Inner2 16 @ wrapper1 # to perform the decorator, i.e., F = wrapper1 (F) = inner1 . 17 DEF F (): 18 is Print ( ' in F ' ) . 19 20 is F () 21 is 22 is # result 23 is >>> wrapper2, before FUNC 24 >>> wrapper1, before FUNC 25 >>> in F 26 is >>> wrapper1, FUNC After 27 >>> wrapper2 ,after func