程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python decorator

編輯:Python

Purpose : Without changing the original function , Modify the function , For example, perform permission verification .

One 、 Function plus decorator without parameter passing

No decorator , Use functions to

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 :

There are ornaments

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

!!! In the decorator a ( Parameters of the outer function ) Meaning of existence , Let the decorator call the incoming function !!!!

Two 、 Functions with parameters plus decorators

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

One parameter :

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 :

  Multiple parameters : Using variable length parameters

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 :

3、 ... and 、 With parameters 、 Functions with return values plus decorators

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 :

Four 、 Use multiple decorators for a parameter

Decoration sequence : First call the decorator close to the decorated function , Then call the decorator far away from the decorated function

The order in which functions are called : First call the decorator far away from the decorated function ( At the top ), Then call the decorator close to the decorated function ( below )

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 :

  5、 ... and 、 Pass parameters in decorator

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 :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved