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

Python 修饰器

时间:2014-07-17 23:19:37      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   art   

描述:对于函数foo,使用修饰器修饰,在执行foo函数的同时统计执行时间。这样其他函数都可以使用此修饰器得到运行时间。

(有返回值和没有返回值的函数要用不同的修饰器似乎)

(对于有返回值的函数,不确定用result存储实际函数执行结果再最终返回的方法是不是恰当)

 1 import time
 2 
 3 def timeit(func):
 4 
 5     def wrapper(word):
 6         start = time.clock()
 7         result = func(word)
 8         end = time.clock()
 9         print Used: , end - start
10         return result
11 
12     return wrapper
13 
14 @timeit
15 def foo(word):
16     return word
17 
18 
19 print foo("123")

 

参考文章:

http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html

Python 修饰器,布布扣,bubuko.com

Python 修饰器

标签:style   blog   http   color   使用   art   

原文地址:http://www.cnblogs.com/mess4u/p/3851358.html

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