python 装饰器

举例:(用装饰器计算函数运行时间)

import time

def fun(func):
    def a(*args, **kwargs):
        start = time.time()
        func(*args, **kwargs)
        end = time.time()
        func(*args, **kwargs)
        print("函数运行时间:%s" % (end-start))
    return a

@fun
def func():
    time.sleep(1)
func()

你可能感兴趣的:(python 装饰器)