装饰器的使用decorator

使用‘@’来使用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)"

你可能感兴趣的:(装饰器的使用decorator)