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

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

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

 import time

 def timeit(func):

     def wrapper(word):
start = time.clock()
result = func(word)
end = time.clock()
print 'Used: ', end - start
return result return wrapper @timeit
def foo(word):
return word print foo("")
 def transfer(func):
def wrapper():
result = func() result_new = {}
for key in result:
result_new[key] = result[key]
if type(result_new[key]) is type("H"):
result_new[key] = result_new[key].upper() return result_new return wrapper @transfer
def foo():
result = {}
result['name'] = "wang"
result['age'] = 1 return result print foo()

在类里

 class test:
def __init__(self):
pass def transfer(func):
def wrapper(instance):
result = func(instance)
result_new = {}
for key in result:
result_new[key] = result[key].upper() return result_new return wrapper @transfer
def getDict(self):
result = {}
result["name"] = "wang"
return result t = test()
print t.getDict()
class test:
def __init__(self):
pass def transfer(func):
def wrapper(instance):
result = func(instance)
result_new = {}
for key in result:
result_new[key] = result[key].upper() return result_new return wrapper @transfer
def getDict(self):
result = {}
result["name"] = "wang"
return result t = test()
print t.getDict()

参考文章:

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

Python 修饰器的更多相关文章

  1. Python修饰器的函数式编程

    Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...

  2. Python修饰器

    Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...

  3. Python修饰器的函数式编程(转)

    From:http://coolshell.cn/articles/11265.html 作者:陈皓 Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Desi ...

  4. 谈谈python修饰器

    前言 对python的修饰器的理解一直停留在"使用修饰器把函数注册为事件的处理程序"的层次,也是一知半解:这样拖着不是办法,索性今天好好整理一下关于python修饰器的概念及用法. ...

  5. python 修饰器 最好的讲解

    Python的修饰器的英文名叫Decorator,修饰器就是对一个已有的模块做一些“修饰工作”,比如在现有的模块加上一些小装饰(一些小功能,这些小功能可能好多模块都会用到),但又不让这个小装饰(小功能 ...

  6. python修饰器(装饰器)以及wraps

    Python装饰器(decorator)是在程序开发中经常使用到的功能,合理使用装饰器,能让我们的程序如虎添翼. 装饰器的引入 初期及问题的诞生 假如现在在一个公司,有A B C三个业务部门,还有S一 ...

  7. python 修饰器(decorator)

    转载:Python之修饰器 - 知乎 (zhihu.com) 什么是修饰器,为什么叫修饰器 修饰器英文是Decorator, 我们假设这样一种场景:古老的代码中有几个很是复杂的函数F1.F2.F3.. ...

  8. Python修饰器讲解

    转自:http://www.cnblogs.com/rollenholt/archive/2012/05/02/2479833.html 文章先由stackoverflow上面的一个问题引起吧,如果使 ...

  9. python修饰器各种实用方法

    This page is meant to be a central repository of decorator code pieces, whether useful or not <wi ...

随机推荐

  1. easyui valid

    /** * 包含easyui的扩展和常用的方法 * * @author * * @version 20120806 */ var wjc = $.extend({}, wjc);/* 定义全局对象,类 ...

  2. Xcode6中使用initWithTitle:title image:image selectedImage:自定义图片

    使用xcode6来运行项目,发现使用原生的tabbar上的图片不显示了. 这个问题是因为xcode6中的一些api方法被废弃了,同时tabbar上图片的渲染方式发生了改变. 先看xcode6中的tab ...

  3. MyEclipse高效开发之必备快捷键技能

    学习了Java之后,使用MyEclipse开发已经有一段时间了,奈何MyEclipse的界面是英文版的,很多功能都不了解,对于那些英文,每次在调程序的时候,都需要一个一个的查,效率很是低下.于是,就想 ...

  4. 垂死挣扎还是涅槃重生 -- Delphi XE5 公布会归来感想

    Delphi 是一个基本上被我遗忘的工具, 要不是在使用RapidSql , 我是收不到Embarcadero 公司发出的邀请来參加Delphi XE5的公布会的. 有人可能要问为什么是Embarca ...

  5. android106 C基本数据类型

    #JNI java native interface #c的基本数据类型 * int:32位,能表示的数字是2的32次方个 * 最高位用来表示符号位,那么还剩下31位可以表示数值,所以能表示的数字就是 ...

  6. 深入理解计算机系统第二版习题解答CSAPP 2.1

    A.将0x39A7F8转换为二进制. 0011 1001  1010 0111  1111 1000 B.将二进制1100 1001 0111 1011转换为十六进制. 0xC97B C.将0xD5E ...

  7. centos6 install mplayer(multimedia)

    step_1 http://wiki.centos.org/AdditionalResources/Repositories/RPMForge step_2 http://wiki.centos.or ...

  8. Verilog-1995 VS Verilog-2001

    http://www.cnblogs.com/tshell/p/3236476.html 2001年3月IEEE正式批准了Verilog‐2001标准(IEEE1364‐2001),与Verilog‐ ...

  9. jQuery中的html,val,text区别

    在项目开发中,写jQuery代码有时候会搞混淆一下东西,现在写一下demo来列出jQuery的.html(),.text(),.val()的区别. 1. html()取得第一个匹配元素的内容,简单来说 ...

  10. VS 2013 Chrome PPAPI 开发环境

    当前系统版本为 Windows 8.1 x64, Chrome 版本为 50.0 1. 准备工作 下载并安装 Python https://www.python.org/download/ * 必须使 ...