python装饰器

设计一个decorator装饰器,它可作用于任何函数上,并打印该函数的执行时间:

import time

def deco(func):

    def wrapper(*args, **kw):

        start_time = time.time()

        func(*args, **kw)

        end_time = time.time()

        print('the function %s runed time is %s' % (func.__name__, (end_time - start_time)))

    return wrapper

@deco

def test():

    time.sleep(2)

    print('this is test func')

test()

 

原文地址:https://www.cnblogs.com/shiguoqiang/p/10392235.html