[Python-ideas] for/else statements considered harmful

Mike Meyer mwm at mired.org
Thu Jun 7 18:29:01 CEST 2012


On Thu, 7 Jun 2012 11:52:10 -0400
Alice Bevan–McGregor <alice at gothcandy.com> wrote:

> On 2012-06-07 15:30:11 +0000, Mike Meyer said:
> > Calling it "wrap-up processing" seems likely to cause people to think
> > about it as meaning "finally". But if the else clause is not executed
> > if the except clause is (as done by try/except/else), then there's no
> > longer an easy way to describe it.
> > 
> > It seems like adding an except would change the conditions under which
> > the else clause is executed (unlike try/except/else), as otherwise
> > there's no easy way capture the current behavior, where else is
> > executed whenever there are no chunks left to process. But that kind
> > of things seems like a way to introduce bugs.
> 
> Well, how about:
> 
>     for <var> in <iterable>:
>         pass # process each <var>
>     except:  # no arguments!
>         pass # nothing to process
>     else:
>         pass # fell through
>     finally:
>         pass # regardless of break/fallthrough/empty
> 
> Now for loops perfectly match try/except/else/finally!  >:D  (Like 
> exception handling, finally would be called even with an inner return 
> from any of the prior sections.)

For for (and don't forget while) loops, finally is pointless. It's the
same as code after the loop. For try, finally runs even if there's an
exception, which isn't true of that code.

     <mike
-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/
Independent Software developer/SCM consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org



More information about the Python-ideas mailing list