# 类装饰器之:加在类上的装饰器 # def auth(func): # def inner(*args, **kwargs): # print("我要开始了") # res = func(*args, **kwargs) # res.name='lqz' # return res # # return inner # # # @auth # Person=auth(Person) # 加在类上的装饰器 # class Person(): # pass # # p=Person() # print(p.name) # 类装饰器之:装饰器是类 class Auth(): def __init__(self, func): self.func = func def __call__(self, a, b): print('我要开始加了') res = self.func(a, b) return res @Auth # add=Auth(add) def add(a, b): # add 是Auth的对象 return a + b res=add(4,5) # add加括号,会触发Auth的__call__ print(res)
标签:__,res,Auth,add,func,装饰,def From: https://www.cnblogs.com/shangxin-bai/p/16985920.html