Иногда мне нужен следующий шаблон в для
петля. Иногда более одного раза в одном и том же цикле:
try:
var = 'attempt to do something that may fail on a number of levels'
except Exception, e:
log(e)
continue
Теперь я не вижу хорошего способа обернуть это в функцию, поскольку он не может return continue
:
def attempt(this):
try:
return this
except Exception, e:
log(e)
# 1. continue # <-- syntax error: continue not properly in loop or
# 2. return continue # <-- invalid syntax
# 3.
return False # <-- this sort of works, but makes me feel powerless
If I return Ложь
, чем могла бы:
var = attempt('to do something that may fail on a number of levels')
if not var:
continue
Но я не думаю, что это справедливо. Я хочу указать циклу for continue
(или подделать его) из функции попытки
.