装饰器1
def lo_stat(msg=None):
print(f"{msg} 启动")
def lo_end(msg=None):
print(f"{msg} 结束")
def decorator(func):
def wrapper():
# print("在函数执行之前做点事")
lo_stat(func.__name__)
func() # 调用被装饰的函数
lo_end(func.__name__)
# print("在函数执行之后做点事")
return wrapper
@decorator
def hello():
print("hello world")
def log(func):
def wrapper(*args, **kwargs):
print(f"调用 {func.__name__} 函数")
return func(*args, **kwargs)
return wrapper
@log
def add(a, b):
return a + b
if __name__ == '__main__':
hello()
print(add(10, 20))
本文来自博客园,作者:__username,转载请注明原文链接:https://www.cnblogs.com/code3/p/18552091