首页 > 编程语言 > 详细

python装饰器小例子

时间:2017-07-02 21:38:11      阅读:204      评论:0      收藏:0      [点我收藏+]
import time

def timer(func): #timer(test1) func=test1
def deco(*args,**kwargs):
start_time=time.time()
func(*args,**kwargs)
stop_time=time.time()
print("the func run time %s" %(stop_time-start_time))
return deco

@timer #test1=timer(test1)
def test1():
time.sleep(3)
print("in the test1")

@timer
def test2(name,age):
time.sleep(2)
print("in the test2" ,name,age)


test1()
test2("joan",5)


输出结果:

in the test1
the func run time 3.0043931007385254
in the test2 joan 5
the func run time 2.0034029483795166

python装饰器小例子

原文:http://www.cnblogs.com/joangaga/p/7107243.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!