码迷,mamicode.com
首页 > 编程语言 > 详细

Python装饰器简单实例

时间:2015-11-20 00:05:34      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

#!/usr/bin/env python
__author__ = ‘氨蛋三键


class Tracer:

    def __init__(self, fun):
        self.calls = 0
        self.fun = fun

    def __call__(self, *args, **kwargs):
        self.calls += 1
        print("Tracer 第 %s 次调用的函数是 %s " % (self.calls, self.fun.__name__))
        self.fun(*args, **kwargs)


class TestDemo:

    @Tracer
    def fun(*args, **kwargs):
        print(*args, **kwargs)

if __name__ == "__main__":
    td = TestDemo()
    td.fun(1, 2, 3, 4, 5, 6, 7)

执行结果是:

Tracer 第 1 次调用的函数是 fun 
1 2 3 4 5 6 7

  

Python装饰器简单实例

标签:

原文地址:http://www.cnblogs.com/shiluocn/p/4979400.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!