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

Python decorator

編輯:Python
""" Open and closed principle : to open up : Code expansion is open closed : Changes to the source code are closed Decorator : The decorator itself is a function , The essence of decorators is closures . Decorators follow the principle of opening and closing . When decorators decorate a function , Without changing the source code of the original function and the way of calling , Add additional new functions to the original function """
import time
# 1、 Original decorator 
def login_csdn():
time.sleep(2)
print(' Welcome to login CSDN Community ')
def time_test(func): # func = login_csdn Address of function 
""" Test login csdn The execution efficiency of community functions """
def inner():
start_time = time.time()
func() # login_csdn() This is the closure , Reference of inner function to non global variable of outer function 
end_time = time.time()
print(f' The execution efficiency of this function is :{
end_time - start_time}')
return inner
login_csdn = time_test(login_csdn) # On the left login_csdn The variable gets inner The memory address of the function 
login_csdn() # inner()
# 2、 Standard Edition decorator : Decorator functions should be written at the top of all decorated functions ( The decorated function in this version has no return value , But a function decorated with a return value cannot get a return value , Not perfect )
def time_test(func): # func = login_csdn Address of function 
""" Test login csdn The execution efficiency of community functions """
def inner():
start_time = time.time()
func() # login_csdn() This is the closure , Reference of inner function to non global variable of outer function 
end_time = time.time()
print(f' The execution efficiency of this function is :{
end_time - start_time}')
return inner
# @time_test It's called grammar sugar , Equivalent to this sentence login_csdn = time_test(login_csdn).
# Code execution to “@time_test” In this business , The following will be executed at the same time “def login_csdn():” This line ,
# Get the address of the function name , And then execute login_csdn = time_test(login_csdn) This code , Pass the function name and address to the decorator function time_test
@time_test
def login_csdn():
time.sleep(2)
print(' Welcome to login CSDN Community ')
login_csdn()
# 3、 Standard Edition decorator : Decorator functions should be written at the top of all decorated functions ( The decorated function has a return value )
def time_test(func): # func = login_csdn Address of function 
""" Test login csdn The execution efficiency of community functions """
def inner():
start_time = time.time()
res_return = func() # login_csdn() This is the closure , Reference of inner function to non global variable of outer function 
end_time = time.time()
print(f' The execution efficiency of this function is :{
end_time - start_time}')
return res_return
return inner
# @time_test It's called grammar sugar , Equivalent to this sentence login_csdn = time_test(login_csdn).
# Code execution to “@time_test” In this business , The following will be executed at the same time “def login_csdn():” This line ,
# Get the address of the function name , And then execute login_csdn = time_test(login_csdn) This code , Pass the function name and address to the decorator function time_test
@time_test
def login_csdn():
time.sleep(2)
print(' Welcome to login CSDN Community ')
return '777 I want to return some data '
# ret What you actually receive is an inner function inner The return value of , But the return value of the decorated function is given to the decorator func() The executor of the function 
# So the return value of the inner function only needs to return func() The executor of the function , namely res_return
ret = login_csdn()
print(ret)
# 4、 The most standard decorator : Decorator functions should be written at the top of all decorated functions ( The decorated function has a return value , The decorated function contains parameters )
def time_test(func): # func = login_csdn Address of function 
""" Test login csdn The execution efficiency of community functions """
def inner(*args, **kwargs): # Function definition ,* For aggregation 
start_time = time.time()
# Function call ,* To break up , func(*(' Su Qin ', 17)) == func(' Su Qin ', 17) == login_csdn(' Su Qin ', 17)
res_return = func(*args, **kwargs) # login_csdn() This is the closure , Reference of inner function to non global variable of outer function .
end_time = time.time()
print(f' The execution efficiency of this function is :{
end_time - start_time}')
return res_return
return inner
# @time_test It's called grammar sugar , Equivalent to this sentence login_csdn = time_test(login_csdn).
# Code execution to “@time_test” In this business , The following will be executed at the same time “def login_csdn():” This line ,
# Get the address of the function name , And then execute login_csdn = time_test(login_csdn) This code , Pass the function name and address to the decorator function time_test
@time_test
def login_csdn(name, age):
time.sleep(2)
print(f' welcome {
name} Sign in CSDN Community , This year, {
age} year !!!')
return '777 I want to return some data '
# ret What you actually receive is an inner function inner The return value of , But the return value of the decorated function is given to the decorator func() The executor of the function 
# So the return value of the inner function only needs to return func() The executor of the function , namely res_return
ret = login_csdn(' Su Qin ', 17)
print(ret)
# 5、 Final standard decorator template 
def wrapper(func):
def inner(*args, **kwargs):
""" Add here : Perform the operation before the decorated function """
ret = func(*args, **kwargs)
""" Add here : Perform the operation after the decorated function """
return ret
return inner

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