Discuss / Python / 运行前后打印'begin call'和'end call'

运行前后打印'begin call'和'end call'

Topic source
# !/usr/bin/env python3
# -*- coding: utf-8 -*-

#请编写一个decorator
#能在函数调用的前后打印出'begin call'和'end call'的日志

import functools

def log(fn):
	@functools.wraps(fn)
	def wrapper(*arg, **kw):
		print('begin call')
		fm = fn(*arg, **kw)		#此处需要函数运行一遍,
		print(fm)				#打印函数运行结果
		print('end call')
		return fm
	return wrapper

@log
def f(x, y):
	return x + y

f(5, 8) + 9

  • 1

Reply