使用修饰器接受传入参数时,发现参数异常
def catch_error(error_position: str):
def get_params(func):
def save_error(*args):
try:
for i in range(3):
return func(*args)
except:
print("-----错误发生位置:" + error_position + "----具体内容如下:\n" + traceback.format_exc())
return save_error
return get_params
函数调用如下
@catch_error("positon --a")
def a(i,b=None):
print(i)
b = "b"
a(1,b=f"123{b}")
报错
TypeError: save_error() got an unexpected keyword argument 'i'
所以建议使用位置参数
a(1,f"123{b}")
的形式
问题解决