[Python-ideas] Retrying EAFP without DRY
Nick Coghlan
ncoghlan at gmail.com
Mon Jan 23 06:07:01 CET 2012
On Mon, Jan 23, 2012 at 2:34 PM, Stephen J. Turnbull <stephen at xemacs.org> wrote:
> Nick Coghlan writes:
> > On Mon, Jan 23, 2012 at 7:30 AM, Mike Meyer <mwm at mired.org> wrote:
> > > or wrapping a loop around the code when it wasn't really a loop.
> >
> > You want to do the same thing more than once: that's a loop.
>
> That's a question of point of view. If "thing" is thought of as a
> "try" (an operation that might fail), yes, he wants to do the same
> thing a nondeterministic number of times, and in general, more than
> once: it's a loop. If "thing" is thought of as a "block" (which
> isn't a try), then the "things" done with and without exception are
> different: it's not a loop, it's a conditional.
>
> Looking at his example "superficially" (I intend no deprecation by
> that word, just a point of view in looking at the code), I have some
> sympathy for Mike's claim that "it's not a loop and it violates DRY."
> I "know what he means" (and I bet you do, too!) However, when I try
> to define that, even informally, I arrive at the paragraph above, and
> I end up coming down on the side that you can't consistently claim
> that "it's not a loop" *and* claim that "it violates DRY", in some
> deeper sense.
Exactly - in a very real sense, "retry once" is just a special case of
a more general looping pattern: you execute the body either once or
twice, perhaps with some massaging of state between the two attempts.
One way to write it is:
for attempt in (1, 2): # This iterable controls your number of attempts
try:
# Do whatever
except ExpectedException:
x = prevent_expected_exception(x)
continue # continue == retry
break # break == success (just like a search loop)
else:
raise Exception("Operation failed (made %d attempts)" % attempt)
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list