Python完善decorator
来源:互联网 发布:长相忆五色石南叶 知乎 编辑:程序博客网 时间:2024/06/06 20:43
任务
请思考带参数的@decorator,@functools.wraps应该放置在哪:
def performance(unit): def perf_decorator(f): def wrapper(*args, **kw): ??? return wrapper return perf_decorator
- ?不会了怎么办
注意@functools.wraps应该作用在返回的新函数上。
参考代码:
import time, functoolsdef performance(unit): def perf_decorator(f): @functools.wraps(f) def wrapper(*args, **kw): t1 = time.time() r = f(*args, **kw) t2 = time.time() t = (t2 - t1) * 1000 if unit=='ms' else (t2 - t1) print 'call %s() in %f %s' % (f.__name__, t, unit) return r return wrapper return perf_decorator@performance('ms')def factorial(n): return reduce(lambda x,y: x*y, range(1, n+1))print factorial.__name__
1 0
- Python完善decorator
- python decorator
- python decorator
- python decorator
- python decorator
- Python - Decorator
- Python decorator
- python decorator
- python decorator
- Python Decorator
- Python Decorator
- python decorator
- Python Decorator
- python decorator
- Python - decorator
- Python Decorator
- Python - Decorator
- python decorator
- Java生成图形化验证码
- 细说存储过程
- cocos2dx3.6 弹出对话框的实现
- JAVA基础第四天
- 详解js闭包
- Python完善decorator
- Canvas基本操作
- 利用excel批量修改字段长度
- 调整数组顺序使奇数位于偶数前面
- Lua 与C/C++ 交互系列:利用模板技术在Lua Code中注册C++类
- 对智能指针auto_ptr 的学习
- excel操作工具(03,07,10)
- OJ-类模版
- Server at localhost was unable to start within 45 seconds