[Python-ideas] for/else syntax
Stephen J. Turnbull
stephen at xemacs.org
Fri Oct 2 15:50:17 CEST 2009
Michael Foord writes:
> > +1. File an RFE against PyLint for sure, and against Python too.
> > That's a *great* idea!
>
> Nonsense - there are several other ways to break out of a loop. Raising an
> exception or returning for example.
Sure. Maybe I'm missing something, but consider
for x in xs:
function_that_may_return_or_raise(x,xs)
function_that_should_not_be_executed_in_exceptional_case(xs)
Now, a return or raise cannot cause f_t_s_n_b_e_i_e_c to be
executed; only a break can.[1] Only a break *needs* else.
If you can give a stylistic rule to distinguish when that code should
be spelled instead
for x in xs:
function_that_may_return_or_raise(x,xs)
else:
function_that_should_not_be_executed_in_exceptional_case(xs)
then I agree. However, if you can't, I can't either, and I can offer
no reason why we shouldn't warn about breakless for-else loops since
they are very often not what the user wants, and if they are desired,
they can be spelled equally well without else.
Footnotes:
[1] StopIteration can, but I thought raising StopIteration in user
code is considered quite bad form.
More information about the Python-ideas
mailing list