class A(object):
"""
不带参数的类装饰器
"""
def __init__(self,func):
self.i = 0
self.func = func
def __call__(self,*args,**kwargs):
#do somethings
self.i += 1
return self.func(*args,**kwargs)
@A
def funcA():
print "funcA"
class B(object):
"""
带参数的类装饰器
"""
def __init__(self,*tp):
self.i = 0
self.tp = tp
def __call__(self,func):
self.func = func
def ff(*args,**kwargs):
#do somethings
self.i += 1
print "type:",self.tp
return self.func(*args,**kwargs)
return ff
@B("fuck")
def funcB():
print "funcB"
funcA()
funcB()
python 类装饰器
最新推荐文章于 2024-06-10 15:30:00 发布