1.普通装饰器
def decorate(fun): ''' 普通装饰器 :param fun: :return: ''' def inner(*args,**kwargs): print('调用装饰器之前') fun(*args, **kwargs) print('调用装饰器之后') return inner @decorate #method=decorate(method) def method01(a,b): print('两个参数的和是',a+b) if __name__ == '__main__': method01(1,2)
2.增强装饰器有名带参装饰器
def dynamic_report(target): ''' 带参装饰器 :param target: 装饰器传进来的参数 :return: ''' def decorate(fun): def inner(*args,**kwargs): print('调用时候传进来的参数',target) fun(*args,**kwargs) print('调用后传进来的参数',target) return inner return decorate @dynamic_report('我是装饰器传进来的') def method(a,b): print('method方法',a+b) if __name__ == '__main__': method(1,2)
def dynamic_report(target01,target02): ''' 带参装饰器 :param target: 装饰器传进来的参数 :return: ''' def decorate(fun): def inner(*args,**kwargs): print('调用时候传进来的参数',target01,target02) fun(*args,**kwargs) print('调用后传进来的参数',target01,target02) return inner return decorate @dynamic_report('zxb','zxl') def method(a,b): print('method方法',a+b)