1. 程式人生 > >一個基於python簡單的裝飾器例項

一個基於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())