with suppress(ValueError):
while test_condition:
biglist.remove(222) # element that doesn't exist, raises error. but we dont care abt this error.
# LINE-A: more remove kind of calls where we are dont care abt ValueError(s) raised
# LINE-B: ...
# ...
# LINE-Z: ...
# LINE-1: some statement..
# some more statements...
使用contextlib.suppress
,while循环在出现第一个异常时停止,执行跳到第1行。
Python中是否有一种替代的构造或功能,我们可以忽略上下文中发生的多个错误,并在上下文中从a行到Z行不间断地继续执行。也就是说,如果异常发生在第A行,执行将继续到第B行,而不是跳到第1行。你知道吗
使用multiple-try-except-finally覆盖从A行到Z行的每一行对我来说并不是一个干净的选择,因为它严重影响了可读性。你知道吗
try:
#LINE-A...
except ValueError:
pass
finally:
try:
#LINE-B...
except ValueError:
pass
finally:
#....
用它们自己的with suppress
将A行到Z行的每个都包装起来是一种可能性,但是可读性较差,所以我想问是否有更多的替代方法
如果你想彻底清除清单,你可以试试
如果您希望使用尝试删除每个值,请使用此
这个怎么样?你知道吗
输出:
相关问题 更多 >
编程相关推荐