Decorator for python functional programming
1. Open and closed principle
Simply put, it is open to extension and closed to modification.
In object-oriented programming, various functions are often defined. The use of a function is divided into the definition phase and the use phase. After a function is defined, it may be called in many places. This means that if the code in the definition phase of the function is modified, many places will be affected. At this time, it is easy to affect the collapse of the entire system due to the modification of a small place. So for the modern program development industry, a system Once online, the source code of the system cannot be changed anymore. However, after a system is launched, as the number of users continues to increase, new functions will be added to the expansion of the system.
At this time, the source code of the original system cannot be modified, and new functions must be added to the original system development. This is the open and closed principle of the program development industry. At this time, decorators are needed.
2. What is a decorator
Decorator, as the name suggests, is a tool for decorating and modifying other objects.
So the decorator can be any callable object, and the decorated object can also be any callable object.
3. The role of decorators
Add new functions to the decorated object without modifying the source code and calling method of the decorated object.
in principle:
Do not modify the source code of the decorated object
Do not modify the calling method of the decorated object
aims:
Add new functions to the decorated object.
Example extension:
import time
# Decorator function
def wrapper(func):
def done(*args,**kwargs):
start_time = time.time()func(*args,**kwargs)
stop_time = time.time()print('the func run time is %s'%(stop_time - start_time))return done
# Decorated function 1
@ wrapper
def test1():
time.sleep(1)print("in the test1")
# Decorated function 2
@ wrapper
def test2(name): #1.test2===wrapper(test2)2.test2(name)==dome(name)
time.sleep(2)print("in the test2,the arg is %s"%name)
# transfer
test1()test2("Hello World")
Examples without parameters:
import time
user,passwd ='admin','admin'
def auth(auth_type):print("auth func:",auth_type)
def outer_wrapper(func):
def wrapper(*args,**kwargs):print("wrapper func args:",*args,**kwargs)if auth_type =="local":
username =input("Username:").strip()
password =input("Password:").strip()if user == username and passwd == password:print("3[32;1mUser has passed authentication3[0m")
res =func(*args,**kwargs) # from home
print("---after authenticaion ")return res
else:exit("3[31;1mInvalid username or password3[0m")
elif auth_type =="ldap":print("ldap link")return wrapper
return outer_wrapper
@ auth(auth_type="local") # home =wrapper()
def home():print("welcome to home page")return"from home"
@ auth(auth_type="ldap")
def bbs():print("welcome to bbs page"print(home()) #wrapper()bbs()
This is the end of this article on learning decorators for Python novices. For more introduction to Python decorators, please search for the previous articles of ZaLou.Cn or continue to browse related articles below. Hope you will support ZaLou.Cn more in the future. !
Recommended Posts