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 ...
随机推荐
- UEFI引导修复教程和工具
参考 http://bbs.wuyou.com/forum.php?mod=viewthread&tid=323759 1. MBR分区表:Master Boot Record,即硬盘主引导记 ...
- ubuntu彻底卸载搜狗拼音输入法
ubuntu彻底卸载搜狗拼音输入法,ubuntu安装搜狗输入法后如果觉得搜狗不是很适合自己,那应该怎么样彻底的卸载搜狗输入法呢?下面我们就来一步步彻底卸载掉搜狗输入法... 方法/步骤 1 找到安装的 ...
- iOS 状态栏、导航栏、工具栏、选项卡大小
1.状态栏状态栏一般高度为20像素,在打手机或者显示消息时会放大到40像素高,注意,两倍高度的状态栏在好像只能在纵向的模式下使用.如下图用户可以隐藏状态栏,也可以将状态栏设置为灰色,黑色或者半透明的黑 ...
- [AngularJS + Webpack] Using Webpack for angularjs
1. Install webpack & angular: npm install webpack angular 2. Create webpack.config.js file: modu ...
- mysql选择联合索引还是单索引?索引列应该使用哪一个最有效?深入測试探讨
先建表 CREATE TABLE `menu_employee` ( `Id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键,无实际意义', `emplo ...
- OPTIMIZE TABLE
INNODB 不支持 mysql> OPTIMIZE TABLE t; +--------+----------+----------+----------------------------- ...
- 【转】如何高效利用GitHub——2013-08-28 22
http://www.yangzhiping.com/tech/github.html 正是Github,让社会化编程成为现实.本文尝试谈谈GitHub的文化.技巧与影响. Q1:GitHub是什么 ...
- Ubuntu16.04/windows7修改本地hosts文件
1. 从github上下载最新的hosts文件:https://serve.netsh.org/pub/ipv4-hosts/ ubuntu16.04: 第二步:Ctrl+Alt+T 打开ubuntu ...
- string与StringBuilder之性能比较
知道“StringBuilder比string性能强”好多年了,近日无聊病发作,就把这两个家伙给动了手术: using System; using System.Text; namespace Con ...
- ImageView的属性android:scaleType
ImageView的属性android:scaleType,即 ImageView.setScaleType(ImageView.ScaleType). android:scaleType是控制图片如 ...