Python - 装饰器
2017-12-03 20:44
246 查看
import time import functools def exec_time(func): def wrapper(*args, **kw): print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) return func(*args, **kw) return wrapper @exec_time def testfunc(): print("test function...") testfunc() print(testfunc.__name__) print("=====================") def exec_time(text): def deractor(func): @functools.wraps(func) def wrapper(*args, **kw): print(time.strftime(text, time.localtime(time.time()))) return func(*args, **kw) return wrapper return deractor @exec_time('%Y-%m-%d') def testfunc(): print("test function...") testfunc() print(testfunc.__name__) print("=====================") def exec_time(text): def deractor(func): @functools.wraps(func) def wrapper(*args, **kw): print("begin time:", time.strftime(text, time.localtime(time.time()))) t = func(*args, **kw) print("end time:", time.strftime(text, time.localtime(time.time()))) return t; return wrapper return deractor @exec_time('%Y-%m-%d %H:%M:%S') def testfunc(): print("test function...") testfunc() print(testfunc.__name__)
output:
2017-12-03 20:50:00 test function... wrapper ===================== 2017-12-03 test function... testfunc ===================== begin time: 2017-12-03 20:50:00 test function... end time: 2017-12-03 20:50:00 testfunc
相关文章推荐
- Python__装饰器练习题
- Python中使用装饰器时需要注意的一些问题
- python的描述符(descriptor)、装饰器(property)造成的一个无限递归问题分享
- 第二天 Python3.4.2 函数是第一等公民 装饰器
- 如何理解Python装饰器?
- Python 装饰器浅析
- python_装饰器二(对有/不定长参数函数的装饰)
- Python装饰器实例(2):面向切面编程
- 详解Python装饰器由浅入深
- python装饰器和偏函数联合运用碰到的问题
- 理解Python装饰器(一)
- python中的装饰器
- python使用装饰器捕获异常
- python--闭包,装饰器
- Python基础(装饰器)
- python3--装饰器高级学习版
- Python进阶之装饰器
- Python装饰器学习
- Python 装饰器(Decorator)
- Python 装饰器