from builtins import Exception
class JumpingAround(Exception):
def __init__(self, message=None, data=None, *args, **kwargs):
super(Exception, self).__init__(message, *args, **kwargs)
self.data = data
try: #surrounding the IF/BLOCK you want to jump out of
if (r.status_code == 410):
s_list = ['String A', 'String B', 'String C']
for x in s_list:
if (some condition):
print(x)
# break
raise JumpingAround() # a more complex use case could raise JumpingAround(data=x) when catching in other functions
print('Not Found')
except JumpingAround as boing:
pass
if (r.status_code == 410):
s_list = ['String A', 'String B', 'String C']
found = None
for x in in s_list:
if (some condition):
found = x
print(x)
break
if not found:
print('Not Found')
if (r.status_code == 410):
s_list = ['String A', 'String B', 'String C']
for x in in s_list:
if (some condition):
print(x)
break
else:
print('Not Found')
事实上,语言中有一种通用的机制可以打破任何障碍,你抛出一个hissy-fit,也就是异常;只要确保你抓住它,恢复你的流动。 这完全取决于您所处的环境(编码标准、性能等)
它不像标签那样干净&;在一些语言中,跳转/跳转,但我不介意
为什么不直接使用变量呢
你不能突破
if
语句;但是,您可以使用for
循环的else
子句有条件地执行print
调用print
仅当for
循环在s_list
上迭代时被StopIteration
异常终止时才会调用,而不是,如果它被break
语句终止相关问题 更多 >
编程相关推荐