Python 修饰器
描述:对于函数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 修饰器的更多相关文章
- Python修饰器的函数式编程
Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...
- Python修饰器
Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都 ...
- Python修饰器的函数式编程(转)
From:http://coolshell.cn/articles/11265.html 作者:陈皓 Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Desi ...
- 谈谈python修饰器
前言 对python的修饰器的理解一直停留在"使用修饰器把函数注册为事件的处理程序"的层次,也是一知半解:这样拖着不是办法,索性今天好好整理一下关于python修饰器的概念及用法. ...
- python 修饰器 最好的讲解
Python的修饰器的英文名叫Decorator,修饰器就是对一个已有的模块做一些“修饰工作”,比如在现有的模块加上一些小装饰(一些小功能,这些小功能可能好多模块都会用到),但又不让这个小装饰(小功能 ...
- python修饰器(装饰器)以及wraps
Python装饰器(decorator)是在程序开发中经常使用到的功能,合理使用装饰器,能让我们的程序如虎添翼. 装饰器的引入 初期及问题的诞生 假如现在在一个公司,有A B C三个业务部门,还有S一 ...
- python 修饰器(decorator)
转载:Python之修饰器 - 知乎 (zhihu.com) 什么是修饰器,为什么叫修饰器 修饰器英文是Decorator, 我们假设这样一种场景:古老的代码中有几个很是复杂的函数F1.F2.F3.. ...
- Python修饰器讲解
转自:http://www.cnblogs.com/rollenholt/archive/2012/05/02/2479833.html 文章先由stackoverflow上面的一个问题引起吧,如果使 ...
- python修饰器各种实用方法
This page is meant to be a central repository of decorator code pieces, whether useful or not <wi ...
随机推荐
- 蘑菇街IM
https://github.com/mogutt TTServer 蘑菇街TeamTalk之消息服务器 Updated 2 days ago Shell 122 208 TTAutoDeploy ...
- JAVA Web 之 struts2文件上传下载演示(二)(转)
JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...
- 容器大小的改变以及容器操作可能使迭代器失效、vector对象的容量变化
1 改变容器的大小 我们可以使用resize来增加或缩小容器,与往常一样,array不支持resize.如果当前大小大于所要求的大小,容器后面的元素会被删除:如果当前大小小于新大小,会将新元素添加到容 ...
- 从 setNeedsLayout 说起
本文从 setNeedsLayout 这个方法说起,分享与其相关的 UIKit 视图交互.使用场景等内容. UIKit 为 UIView 提供了这些方法来进行视图的更新与重绘: public func ...
- 删除sd卡的文件
public static void deleteAllFile(){ String path = Environment.getExternalStorageDirectory().getAbsol ...
- Android_Intent_passObject
方法4. 把基本的数据类型封装到一个对象中,然后通过intent传递该对象需要考虑对Person对象进行序列化 MainActivity: package com.example.day06_acti ...
- Objective-c中autorelease的释放时机
如果你使用过MRR,autorelease这个关键字应该是太熟悉了,每次在我们生成一个新的对象返回时,都需要向这个对象发送autorelease消息,目的是为了延时释放创建的对象.那到底是在什么时候, ...
- mysql左外连接,右外连接,全连接
- webform 转 MVC 飞一般的感觉
前言: 浅谈webform与mvc,让开发变得更加简单,这里主要通过比较webform与mvc的开发方式,以下全属个人看法,不完善的地方可以留言补充. 正文: 废话不多说,直接说工作中经常用到的地方 ...
- UITabBarController 初学
纯代码编写UITabBarController, 不多说,直接见代码 RViewController1 *vc1 = [[RViewController1 alloc]init]; UINavigat ...