裝飾器

#! /usr/bin/env python
# -*- coding:utf-8 -*-
import time
def timer(fun):
    def deco(*args, **kwargs):
        start_time = time.time()
        fun(*args, **kwargs)
        stop_time = time.time()
        print("fun cost time:%s" % (stop_time - start_time))
    return deco
@timer # test1 = timer(test1)
def test1():
    time.sleep(1)
    print("this is test1")
@timer
def test2(name):
    time.sleep(1)
    print("this is test2 ", name)

test1()
test2("leif")


參考http://egon09.blog.51cto.com/9161406/1836763

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章