'''
用类实现装饰器
'''
#普通装饰器的实现:
def check(func): #装饰器是一个闭包 把内函数return出去
def inner():
print('validate')
func()
return inner
@check
def publish():
print('publish')
publish()
#用类去实现:
class check():
def __init__(self,func):
self.f=func
def __call__(self, *args, **kwargs):
print('validate')
self.f()
#方法一:
def publish():
print('publish')
check(publish)()
#方法二:
@check
def publish():
print('publish')
publish()
用类实现装饰器
于 2023-11-29 12:01:06 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)