Discuss / Python / 记录-装饰器

记录-装饰器

Topic source
import time, functools

def decorator(fn):
    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        t = time.time()
        fn()
        print(f'cost:{time.time() - t:.4f}s')
    return wrapper

@decoratordef fn():
    print('Begin')
    time.sleep(1)
    print('End')

fn()

  • 1

Reply