Decorators do it

********************** ********************** layer formed decorators ************

def wrapper1(func):

def inner1(*args,**kwargs):

print(1)

func(*args,**kwargs)

print(11)

return inner1

@wrapper1

def foo():

print(8)

@ Wrapper1 this magic hall is a disguised means: @ the following function (which is to be decorated function name) as a parameter passed into the back of the @ function, the

Masquerading function is then decorated with the way out

Decorator process consisting of:

Indirect can wapper (inner1) within () function can be called, thereby enabling the function to be decorated is reflected in the function.

The idea of ​​finishing the entire decorator who wants to perform the function is decorative function being wrapped, we must first call up the main function, or simply get in,

First, write a wrapper1 (foo) (), why write a foo it? Because the main function inside the function you want to call a function that is sure to be decorated with decorative function to introduce,

So we put the name of the function foo (storing the address of the function) on the main function pass in, to be for internal use function, and then look for opportunities to foo () function is called up (function + () function call can be executed )

wrapper1 (foo) () can mobilize the entire function, but worse disguised as foo () if you want to look like that abruptly written foo () how to do it? wrapper1 (foo) () = foo ()?

inner1 function started to put our decorative function calls are up, but to start inner1 must first start wrapper1 this function for the job (as wrapper can take advantage of the return) and then start the function inner1 function () is executed decorative function; and foo can only become inner1 then add () function can be called from being executed,

The name would have become inner1 foo job, inner1 is wrapper1 return value (), that is to wrapper1 () function is called up can get a inner1: wrapper1 () == inner1

I have to let inner1 inside before we can call foo () function how to do, it is not necessary to foo (function address) pass in?

That is the wrapper1 (foo) == inner that I find just a **** "name" = wrapper1 (foo) *** together with a () on the success of the function calls from the inner1,

That order and the decorative function (foo ()) to maintain the same shell would put the "name" with foo, and thus constitutes a new function is decorative function (foo ()) exactly the same shape

foo (), and ensure the consistency of form.

** ** ** ** ** ** ** ** ** ** ** ** form two decorator ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **

def wrapper1(func):

def inner1(*args,**kwargs):

print(1)

func(*args,**kwargs)

print(11)

return inner1

def wrapper2(func):

def inner2(*args,**kwargs):

func(*args, **kwargs)

print(22)

return inner2

# @wrapper1

# @wrapper2

def foo():

print(8)

This means that in the above plus a decorator decorator

Follow the steps syntax is the first @ wrapper1 following function decorated, but the following is also a @ wrapper2 decorative function, then it must first figure out @ wrapper2 this decorator job ah,

Well @ wrapper2 is to wrapper2 (foo) assigned to the name foo, ie foo = wrapper2 (foo), then to perform a decorative function must be written foo () (ie, the original function wrapper2 (foo) ()) to perform,

This form must then also to function as a function of the wrapper2 again wrapper1 decorate it the same way is not necessary to wrapper2 (foo) pass into wrapper1 in ah namely wrapper1 (wrapper2 (foo)) that is then executed ()

wrapper1(wrapper2(foo))()

** ** ** ** ** formed ** ** ** ** ** ** ** ** three decorator ** ** ** ** ** ** ** ** ** ** ** ** ** ** **

def wrapper1(func):

def inner1(*args,**kwargs):

print(1)

func(*args,**kwargs)

print(11)

return inner1

def wrapper2(func):

def inner2(*args,**kwargs):

func(*args, **kwargs)

print(22)

return inner2

def wrapper3(func):

def inner3(*args,**kwargs):

print(3)

func(*args, **kwargs)

print(33)

return inner3

# @wrapper1

# @wrapper2

# @wrapper3 # 等同于 wrapper1(wrapper2(wrapper3(foo)))()

def foo():

print(8)

Decorator with parameters

Setting an identification symbol

flag = True

def auth(arg):

def func(f):

def inner(*args,**kwargs):

if flag:

print ( 'decorate')

return f()

# Print ( 'come about')

else:

f()

return inner

return func

@auth(flag)

def foo():

print ( 'which is posted back a')

foo()

Guess you like

Origin www.cnblogs.com/wing3993/p/11261855.html