我想编写一个python修饰符,这样一个引发异常的函数将再次运行,直到它成功,或者达到放弃之前的最大尝试次数。你知道吗
像这样:
def tryagain(func):
def retrier(*args,**kwargs,attempts=MAXIMUM):
try:
return func(*args,**kwargs)
except Exception as e:
if numberofattempts > 0:
logging.error("Failed. Trying again")
return retrier(*args,**kwargs,attempts=attempts-1)
else:
logging.error("Tried %d times and failed, giving up" % MAXIMUM)
raise e
return retrier
我的问题是,我需要一个保证,即无论kwargs包含什么名称,都不能与用于表示尝试次数的名称发生冲突。你知道吗
但是,当函数本身将attempts
作为关键字参数时,这不起作用
@tryagain
def other(a,b,attempts=c):
...
raise Exception
other(x,y,attempts=z)
在本例中,如果运行other,它将运行z次,而不是最大运行次数(请注意,要发生此错误,必须在调用中显式使用关键字参数!)。你知道吗
从函数属性获取重试次数,而不是参数。你知道吗
要使
MAXIMUM
成为调用函数时可以指定的内容,请将定义更改为尽管仍然存在与
attempts
属性发生名称冲突的风险,但是函数属性很少被使用这一事实使得将tryagain
文档化为不使用具有预先存在的attempts
属性的函数更为合理。你知道吗(我把它作为一个练习来修改
tryagain
以获取一个属性名作为参数:以便您可以在装修时选择一个未使用的名称。因此,还可以使用
tryagain
的参数作为要添加到my_func
的关键字参数的名称。)您可以指定decorator参数,如下所示:
相关问题 更多 >
编程相关推荐