def add_1(a):
def add_2():
print('-'*30+' Decoration permission verification ')
a()
return add_2
def check1():
print('{:-^30}'.format('check1'))
check1=add_1(check1)
check1()
Operation process : Will function check1 Pass as parameter to add_1 function ,add_1 Returns the inner function add_2, Transfer memory function add_2 Pass to check1, then check1 Instantiation , It's equivalent to putting add_2 Instantiate run , Print permission verification first during operation , Call the passed in before check1 function .
Print the results :
def add_1(a):
def add_2():
print('-'*30+' Decoration permission verification ')
a()
return add_2
@add_1
def check1():
print('{:-^30}'.format('check1'))
check1()
Print the results :
A comparison of the two , amount to @add_1 Replaced the check1 = add_1(check1) , And move forward to the function definition
The inner function of decorator function is equivalent to the function to be decorated , I rewrote it , So the parameters need to be consistent with the decorated function
def add_1(a):
def add_2(b):
print('-'*30+' Decoration permission verification ')
a(b)
return add_2
@add_1
def check1(num):
print('{:-^30}{}'.format('check1',num))
check1(999)
Print the results :
def add_1(a):
def add_2(b,*args,**kwargs):
print('-'*30+' Decoration permission verification ')
a(b,*args,**kwargs)
return add_2
@add_1
def check1(num,*args,**kwargs):
print('{:-^30}{}'.format('check1',num))
print(args)
print(kwargs)
check1(999,11)
check1(999,11,'jj',f=1,ff=22)
Print the results :
You need to call the function position of the inner function of the original decorator , Go back
def add_1(a):
def add_2(*args,**kwargs):
print('-'*30+' Decoration permission verification ')
return a(*args,**kwargs)
return add_2
@add_1
def check1(*args,**kwargs):
print('*'*30,args)
print(kwargs,'!'*30)
return args,kwargs
print(check1(100))
Print the results :
Just like packing , When packing , Pack the inner layer first , Then pack the outer layer
When using packaged things , First remove the outer layer , Then dismantle the inner layer
def add_1(a):
def add_2(*args,**kwargs):
print('-'*30+' Decoration permission verification 1')
return a(*args,**kwargs)
return add_2
def bba_1(a):
def bba_2(*args,**kwargs):
print('-'*30+' Decoration permission verification 2')
return a(*args,**kwargs)
return bba_2
@bba_1
@add_1
def check1(*args,**kwargs):
print('*'*30,args)
print(kwargs,'!'*30)
return args,kwargs
print(check1(100))
Print the results :
application : Permission verification classification , First level verification ... Secondary verification
In the outer layer of the original decoration function , Add a layer of functions , Receiving parameters , Put inside the function
In the innermost pair , Operate with the passed in parameters
def qqq(num):
def add_1(a):
def add_2(*args,**kwargs):
if num == 1:
print('-'*30+' Decoration permission verification 1')
elif num == 2:
print('*' * 30 + ' Decoration permission verification 2')
else:
print(' No verification , Operate at will ')
return a(*args,**kwargs)
return add_2
return add_1
@qqq(1)
def check1(*args,**kwargs):
print('*'*30,args)
print(kwargs,'!'*30)
return args,kwargs
print(check1(100))
Print the results :
This article is a test develop
Ubuntu下的Nginx+Uwsgi+DjangoProj