Discuss / Python / 能在函数调用的前后打印出'begin call'和'end call'的日志

能在函数调用的前后打印出'begin call'和'end call'的日志

Topic source

小喵biubiu

#1 Created at ... [Delete] [Delete and Lock User]
  1. #一个decorator,能在函数调用的前后打印出'begin call'和'end call'的日志
  2. import time, functools
  3. def metric(fn):
  4. @functools.wraps(fn)
  5. def timer(args, *kwargs):
  6. print('begin call:')
  7. print(fn(args, *kwargs))
  8. return( 'end call.')
  9. #return fn(args, *kwargs)
  10. return timer
  11. @metric
  12. def fast(x, y):
  13. time.sleep(0.0012)
  14. return x + y
  15. print(fast(11,22))

  • 1

Reply