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

# -*-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())
展开阅读全文

没有更多推荐了,返回首页