Discuss / Python / 课后作业

课后作业

Topic source

Ron09900

#1 Created at ... [Delete] [Delete and Lock User]

1、打印执行时间

def metric(fn):
    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        millions = int(round(time.time() * 1000))
        print(f'{fn.__name__} executed in {millions} ms')
        res = fn(*args, **kwargs)
        return res
    return wrapper

2、方法前执行、方法后执行

def metric(fn):    @functools.wraps(fn)    def wrapper(*args, **kwargs):        millions = int(round(time.time() * 1000))        print(f'{fn.__name__} begin call')        res = fn(*args, **kwargs)        print(res)        print(f'{fn.__name__} end call')        return res    return wrapper

  • 1

Reply