Иногда мне нужен следующий шаблон в цикле for
. Время от времени более одного раза в одном цикле:
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
Если я return False
чем я мог:
var = attempt('to do something that may fail on a number of levels')
if not var:
continue
Но я не чувствую, что это справедливость. Я хочу передать цикл for continue
(или подделать его) из функции attempt
.