def safe_func(func):
# 安全运行目标函数,运行错误后记录
def wrapped(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as err:
print(f'output: err ={err},func={func.__name__},args={args},kwargs={kwargs}')
return False
wrapped.__name__ = func.__name__ # 将目标函数名改回原函数名
return wrapped
示例
@safe_func
def add(a,b)
print(a+b)
运行:
print(add.__name__)
print(add(1,'2'))
输出:
add
output: err =unsupported operand type(s) for +: 'int' and 'str',func=add,args=(1, '2'),kwargs={}
False
网友评论