Discuss / Python / @functools.wraps(fn)这句有什么用

@functools.wraps(fn)这句有什么用

Topic source

Carbon

#1 Created at ... [Delete] [Delete and Lock User]
# 学习时间: 2022/9/25 20:10import timedef metric(func):    def wrapper(*args, **kwargs):        start_time = time.time()  # 定制执行语句        ret = func(*args, **kwargs)        print('%s executed in %s ms' % (func.__name__, time.time()-start_time))        return ret    return wrapper@metricdef fast(x, y):    time.sleep(1)    return x + y@metricdef slow(x, y, z):    time.sleep(1)    return x * y * zf = fast(11, 22)s = slow(11, 22, 33)if f != 33:    print('测试失败!')elif s != 7986:    print('测试失败!')else:    print('测试成功!')

  • 1

Reply