[Python-ideas] Retrying EAFP without DRY

Steven D'Aprano steve at pearwood.info
Tue Jan 24 01:00:53 CET 2012


On Mon, Jan 23, 2012 at 01:34:44PM +0900, Stephen J. Turnbull 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.

I'm afraid I don't understand this. What's a block (that isn't a try) 
here, and how does it differ from a thing, and what makes it a 
conditional?

If all you are saying is that the caller may want to abstract out
"repeat this thing until it succeeds (or fails permanently)" into a
single operation without explicitly writing a for or while loop, that's
what functions are for. map(func, seq) doesn't cease to be a loop just
because you don't write it as an explicit loop.

If you mean something different from this, I have no idea what you mean.

 
> 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!)

I don't. To me, "retry this thing repeatedly" is fundamentally a loop. 


> 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.

If it violates DRY, then by definition you must be repeating yourself. 
If you repeat yourself, then the obvious way to avoid repeating yourself 
is to place the repeated code inside a loop. Why is this a problem?

What is especially confusing is that the proposed syntax is *defined* as 
looping back to the start of the try block, like a GOTO. If you draw the 
program flowchart of the construct, it loops backwards. Mike even stated 
that his proposal was exactly identical in behaviour to a try inside a 
while True loop. If he wants to argue that saving one indent level is so 
important that it's worth new syntax, that's one thing, but I am 
perplexed at claims that something which is identical to a loop isn't a 
loop.


-- 
Steven




More information about the Python-ideas mailing list