class tracer:
def __init__(self, func):
self.calls = 0
self.func = func
def __call__(self, *args):
self.calls +=1
print('call %s to %s' % (self.calls, self.func.__name__))
self.func(*args)
@tracer
def spam(a, b, c):
print(a, b, c)
spam(1, 2, 3)
spam('a', 'b', 'c')
spam(4, 5, 6)
运行结果:
call 1 to spam
1 2 3
call 2 to spam
a b c
call 3 to spam
4 5 6
最后
以上就是安静母鸡最近收集整理的关于python 函数装饰器的典型示例的全部内容,更多相关python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复