[Python-ideas] Retrying EAFP without DRY

Mike Meyer mwm at mired.org
Tue Jan 24 19:45:36 CET 2012


On Wed, 25 Jan 2012 05:37:07 +1100
Steven D'Aprano <steve at pearwood.info> wrote:

> Mike Meyer wrote:
> > On Tue, 24 Jan 2012 17:00:28 +1000
> > Nick Coghlan <ncoghlan at gmail.com> wrote:
> > 
> >> On Tue, Jan 24, 2012 at 3:47 PM, Mike Meyer <mwm at mired.org> wrote:
> >>> The argument isn't that we need a new syntax for a small set of
> >>> loops, it's that the only ways to implement retrying after an
> >>> exception leave a code smell.
> >> Uh, saying "retrying is fundamentally a looping operation" is not a
> >> code smell.
> > 
> > No, the code smell is a loop that just gets run through once.
> 
> By this reasoning, "for i in range(n)" is a code smell, because n
> might happen to be 1.

Not quite, because n might also happen to *not* be 1. You could even
run it no times, if n were 0. Nothing wrong with any of that.

> You can't know that the loop will run once until you actually try.

Which is not a code smell. However, if you can tell by reading the
code that it will only run once (or never run), like this one:

     for i in range(1):

Then it's a code smell!

	 <mike



More information about the Python-ideas mailing list