装饰器

装饰器:

import time

def timer(f):
    def inner(a):
        start_time = time.time()
        res = f(1) # 被装饰的函数
        end_time = time.time()
        print(end_time - start_time)
        return res
    return inner

@timer
def func(a): # 传参
    time.sleep(0.1)
    print("11111111",a)
    return 123456

res = func(1) # inner
print(res)

posted @ 2019-12-12 19:18  jaysonzxk  阅读(84)  评论(0编辑  收藏  举报