《Python - 实现一个简单的装饰器》--- 输出程序运行时间
主要结合程序认识理解Python中的装饰器。练习Python代码的编写。简单代码#!/usr/bin/env python#-*- coding: utf-8 -*-"""@author: 烽火@license: Apache Licence@file: decorate.py@time: 7/5/17 3:52 PM"""import time"""定义简单的装饰器,用来输出程
·
主要结合程序认识理解Python中的装饰器。练习Python代码的编写。
简单代码
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
@author: 烽火
@license: Apache Licence
@file: decorate.py
@time: 7/5/17 3:52 PM
"""
import time
"""
定义简单的装饰器,用来输出程序运行的所用时间
"""
def timer(func):
def decor(*args):
start_time = time.time();
func(*args);
end_time = time.time();
d_time = end_time - start_time
print("run the func use : ", d_time)
return decor;
@timer #printSth = timer(printSth) -> printSth = decor
def printSth(str, count):
for i in range(count):
print("%d hello,%s!"%(i,str))
printSth("world", 100)
运行结果
更多推荐
已为社区贡献4条内容
所有评论(0)