Discuss / Python / 装饰器习题解答

装饰器习题解答

Topic source

-反派-

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

import time,functools

def metric(fn): @functools.wraps(fn) def wrapper(*args,**kw): begin = time.time() res = fn(*args,**kw) print('%s excuted in %s ms' % (fn.__name__,time.time()-begin)) return res return wrapper

@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

print(fast(11, 22)) print(slow(11, 22, 33))


  • 1

Reply