[Python-ideas] Retrying EAFP without DRY (original) (raw)
Mike Meyer mwm at mired.org
Sat Jan 21 01:36:48 CET 2012
- Previous message: [Python-ideas] Special method to delegate PEP 3118 buffer access to another object?
- Next message: [Python-ideas] Retrying EAFP without DRY
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Just running another half-baked "easy" idea up to see what others think of it.
One of the cases where EAFP is clumsy is if the "forgiveness" portion is actually retrying what failed after fixing things. I just ran into this in real code, and the resulting discussion suggested this change.
Eiffel's exception-handling mechanism is built assuming that's the standard way to do things. Seems to work ok, but if I had to choose one of the two, I'll take try/except.
So here's the proposal:
A single new keyword/clause, "retry". It has the same syntax as an "except" clause, can be used anywhere "except" can be used, and can be intermingled with them in the same try statement. There's probably a better syntax, but this is easy to describe.
The behavior change from except is that instead of exiting the "try" statement when the "retry" clause ends, it restarts the try clause. In python, this code:
try:
# try block
except:
# except block
retry:
# retry block
else:
# else block
finally:
# finally blockWould provide the same behavior as this code:
while True:
try:
# try block
except:
# except block
retry:
# retry block
else:
# else block
finally:
# finally block
breakWhere except & retry can be repeated, include exceptions to check against, and intermixed with each other. The break would propagate up to the else block and any except blocks if there was no finally. If there's no else, the break from it winds up at the end of the try.
The use case, as mentioned, is avoiding doing EAFP without repeating the code in the exception handler. I.e., the choices without retry are things like (LBYL):
if not isinstance(x, my_class):
x = fixup(x)
mangle(x)
other_stuff(x)or EAFP:
try:
mangle(x)
except:
x = fixup(x)
mangle(x)
other_stuff(x)or:
while True:
try:
mangle(x)
break
except:
x = fixup(x)
other_stuff(x)with:
try:
mangle(x)
retry:
e = fixup(x)
other_stuff(x)My gut reaction is cool, but not clear it's worth a new keyword. So maybe there's a tweak to the except clause that would have the same effect, but I don't see anything obvious.
<mike- Previous message: [Python-ideas] Special method to delegate PEP 3118 buffer access to another object?
- Next message: [Python-ideas] Retrying EAFP without DRY
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]