[Python-ideas] Retrying EAFP without DRY

Terry Reedy tjreedy at udel.edu
Wed Jan 25 04:33:19 CET 2012

On 1/24/2012 7:52 PM, Mike Meyer wrote:

> For instance, I would write your example like so:
>      try:
>          walk_iter = os.walk(topdir)
>          dirpath, files, subdirs = next(wi)
>          # Special case the top level directory
>      except StopIteration:
>          raise RuntimeError("No dir entry for {!r}".format(topdir))
>      # other exception handling here, as appropriate.
>      for dirpath, files, subdirs in walk_iter:
>          # Process the subdirectories

I would too, but ...

> Likewise, the try statement is very powerful. That break/continue may
> or may not be evil is immaterial. The point is to make the try
> statement more powerful.

I disagree with turning 'try' into a specialized loop construct by 
adding a version of continue, which is a version of goto. We currently 
have a general overt loop (while), the covert equivalent (tail 
recursion), and a 'specialized' loop (for) that covers the majority of 
loop needs, leaving the generalized loop (while) to cover everything else.

A 'try' statement in itself it a glorified label statement that also 
sets up the context for exception statements. Exception statement are 
conditional statements where the condition is an exception state. A 
marked program position plus a conditional jump is what makes a loop.

Terry Jan Reedy

More information about the Python-ideas mailing list