''' 开放封闭原则: 开放扩展 封闭:不能修改源功能代码 不能改变调用方式 ''' import time # from functools import wraps def outer(flag): def warpper(f): # @wraps(f) def inner(*args,**kwargs): """ 装饰函数 :param args: :param kwargs: :return: """ if flag: start=time.time() r=f(*args,**kwargs) end=time.time() print(end-start) return r else: r = f(*args, **kwargs) return r return inner return warpper @outer(1)#加括号先调用返回warpper成了@warpper def func(s): print(s) print("原功能函数") time.sleep(2) func("这是主功能原函数") print(func.__doc__) # print(func.__name__)(如果想要显示被装饰函数的函数名或者注释需要导入wraps模块) from functools import wraps def wrapper1(f): def inner(*args,**kwargs): print(1) res=f(*args,**kwargs) print(f.__name__) print(1.1) return res return inner def wrapper2(f): @wraps(f) def inner(*args, **kwargs): print(2) res = f(*args, **kwargs) print(2.1) return res return inner @wrapper1 @wrapper2 def func1(): print(0) func1()