博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个用于统计函数调用消耗时间的装饰器
阅读量:7242 次
发布时间:2019-06-29

本文共 734 字,大约阅读时间需要 2 分钟。

装饰器前面提过了,采用python的闭包特性实现:

from time import timefrom time import sleepdef count_time():    def tmp(func):        def wrapped(*args, **kargs):            begin_time = time()            result = func(*args, **kargs)            end_time = time()            cost_time = end_time - begin_time            print '%s called cost time : %s' %(func.__name__, cost_time)            return result        return wrapped    return tmp@count_time()def test():    sleep(0.5)if __name__ == '__main__':    test()

我们尝试以下的代码:

class Test:    @count_time()    def test(self):        print 'haha'if __name__ == '__main__':#    test()    a = Test()    a.test()

代码仍然可以正常工作,因为a.test()仅仅就是给test添加了一个额外的参数a而已。

转载于:https://www.cnblogs.com/inevermore/p/4194584.html

你可能感兴趣的文章
公有云产品试用介绍
查看>>
我的友情链接
查看>>
Servlet+JSP+MySQL实现用户管理模块之六、实现用户信息显示
查看>>
软件项目管理
查看>>
3012.脚本作业—l201.10.0编写一个脚本用于检测IP地址(递进版10)
查看>>
rpmbuild SPEC文件
查看>>
心在山水间
查看>>
ionic开发android app步骤
查看>>
【数据结构】位图BitMap与布隆过滤器BloomFilter
查看>>
mysql主从 主主
查看>>
Java中FileInputStream和FileOutputStream类实现文件夹及文件的复制粘贴
查看>>
tomcat+jdk部署
查看>>
Toast源码深度分析
查看>>
zabbix监控公网机器
查看>>
python requests模块详解
查看>>
PHP应用架构演化
查看>>
Python定义全局变量的用法
查看>>
RESTful API使用详解
查看>>
linux下php扩展ssh2的详解
查看>>
final关键字(最终的)
查看>>