python basis XXIII: decorator

Decorator

Decorator: essentially a function, a function to add functionality without changing the original function code

Decorator is actually a closure function

Commonly used in: rights verification, user login register

Decorator Category: General decorator

      With a decorative function parameters (fixed length, variable length)

      With a decorative function returns a value of

      Decorator with parameters (different functions of different effects)

      The class as a decorator (hereinafter speaking)

      Decorative (talk back)

Common decorator Method 1

def xdl (func): # decorator 
def inner (): # is the next function after a decorative
print ( 'school punch')
FUNC () #func original function, the function is bj_work, sh_work function
print ( 'school punch')
Inner return
# Beijing's punch function (original function)
DEF bj_work ():
Print ( 'Beijing beginning of a day of learning')
bj_work = XDL (bj_work) #bi_work function == inner function
bj_work ()
# Shanghai's punches 
DEF sh_work ():
Print ( 'Shanghai day began learning')
sh_work = XDL (sh_work)
sh_work ()
Outputs:

Punch school
begin learning Beijing day
after school punch
school punch
to start in Shanghai a day of learning
after school punch

Common methods decorator 2
@ Syntax: @ system will automatically add the following parameters are passed to the function as a decorator, from the lower to the
@ character is also known as syntactic sugar
def xdl (func): # decorator 
def inner (): # is the next function after a decorative
print ( 'school punch')
FUNC () #func original function, the function is hz_work
print ( 'school punch')
return Inner
@xdl 
DEF hz_work ():
Print ( 'Hangzhou day start learning')
hz_work ()
will output:

Punch school
start Hangzhou day learning
school punch

@ The system will automatically add the following parameters to function as a decorator, from bottom to top
DEF out_1 (FUNC): 
DEF Inner ():
Print ( 'start clocking. 1')
FUNC ()
Print ( 'school punch. 1')
return Inner

DEF OUT_2 (FUNC):
DEF Inner ():
Print ( 'start punch 2' )
FUNC ()
Print ( 'school punch 2')
return Inner
@ out_1
@ OUT_2
DEF xqgz ():
Print ( 'beginning of each study day cells')
xqgz ()
will output:

Punch 1 Start
Start punch 2
beginning of each study day cell
school punch 2
school punch 1

Inner # decorator with parameters (fixed length) parameter () must be in the parameters () in the same func, it may not be the same with parameters () in sz_work
DEF XDL (func):
DEF Inner (n-, S ):
Print ( 'school punch')
FUNC (n-, S)
Print ( 'school punch')
return Inner
@xdl
DEF sz_work (name, Sex):
Print ( 'learning start day Shenzhen')
sz_work ( 'Shenzhen', 'city')

will output:

Punch school
start learning Shenzhen day
after school punch

Decorator inner # with parameters (variable length) of () the parameters must be parameter () in the same func, can not with parameters () in sz_work as
DEF XDL (func):
DEF Inner (* A, K **):
Print ( 'school punch')
FUNC (A *, ** K)
Print ( 'school punch')
return Inner
@xdl
DEF sz_work (Arge *, ** kwargs):
Print (Arge, kwargs, ' Shenzhen day began learning ')
sz_work (' Shenzhen ',' city ', waihao =' Windows of the World ')
will output:

School punch
( 'Shenzhen', 'City') { 'waihao': 'Window of the World'} Shenzhen day start learning
after school punch

 

# Decorator with a return value
DEF XDL (FUNC):
DEF Inner ():
Print ( 'start punch')
zhuangtai = FUNC ()
Print ( 'school punch')
return zhuangtai
return Inner

@xdl
DEF Work ():
Print ( 'start work')
return 'work ending'
RES = work ()
Print (RES)
will output:

Punch began
to work
after school punch
end of the work

Decorator with parameters (different functions have different effects)

def wai_out (city): # is just a shell 
def out (func): # real decorator
DEF Inner ():
IF City == 'Shanghai':
Print ( 'start clocking')
FUNC ()
Print ( 'school punch ')
elif City ==' Beijing ':
Print (' Beijing not in class ')
return Inner
return OUT
@wai_out (' Shanghai ')
DEF sh_work ():
Print (' Shanghai began a day of learning ')
sh_work ()
Print (' =============== ')
@wai_out (' Beijing ')
DEF bj_work ():
Print (' Beijing began a day of learning ')
bj_work ()

Guess you like

Origin www.cnblogs.com/szc-boke/p/11276259.html