您的位置:首页 > 编程语言 > Python开发

一个基于python简单的装饰器实例

2017-10-18 18:18 579 查看
# -*-coding:utf-8-*-
# author:murongtiedan
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   #这个装饰器相当于  myfunc =deco(myfunc)
def myfunc():
print("start myfunc")
time.sleep(0.6)
print("end myfunc")

# print("myfunc is:",myfunc.__name__)
# myfunc =deco(myfunc)
# print("myfunc is:",myfunc.__name__)
# print(myfunc())
print(myfunc())
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  实例 装饰器