装饰器的使用decorator

2018-01-15  本文已影响0人  Future石

使用‘@’来使用decorator
'''

coding=utf-8

import time

def deco(func):
def wrapper():
starttime = time.time()
func()
endtime = time.time()
msecs = (endtime - starttime)*1000
print (">elapsed time: %f ms" % msecs)
return wrapper

@deco
def myfunc():
print 'starttime'
time.sleep(0.6)
print 'endtime'

print "myfunc is ",myfunc.name

myfunc()
'''
"@deco" 的本质就是"myfunc = deco(myfunc)"

上一篇 下一篇

猜你喜欢

热点阅读