Discuss / Python / 为什么还要 return fn(*a,**kw)

为什么还要 return fn(*a,**kw)

Topic source

Pklue

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

import time, functools

def metric(fn):

    @functools.wraps(fn)

    def wraps(*a, **kw):

        start = time.time()

        fn(*a, **kw)

        end = time.time()

        ex = (end - start)*1000 

        print('%s executed in %s ms' % (fn.__name__, ex))

        return fn(*a,**kw)

    return wraps

# 测试

@metric

def fast(x, y):

    time.sleep(0.0012)

    return x + y;

@metric

def slow(x, y, z):

    time.sleep(0.1234)

    return x * y * z;

f = fast(11, 22)

s = slow(11, 22, 33)

if f != 33:

    print('测试失败!')

elif s != 7986:

    print('测试失败!')

层主牛逼!!


  • 1

Reply