[Python-ideas] SyntaxWarning for for/while/else without break or return?

Yuvgoog Greenle ubershmekel at gmail.com
Thu Oct 8 11:24:46 CEST 2009

On Thu, Oct 8, 2009 at 10:57 AM, Stefan Rank <list-ener at strank.info> wrote:

> a suggestion/question related to the discussion about renaming for/else and
> while/else:
> Is it easy to generate a SyntaxWarning if you use these constructs without
> a break or return statement in the loop? AFAICS, any such usage would be
> either wrong or unnecessary. (right?)
Your question is good and valid except that "return" has nothing to do with

In order to understand the "else" clause it's better that you think of it as
an "if not break" after the loop. Any way you look at it "return" behaves as
expected - skips the "else" AND skips all the code that follows (much like
"raise" would have). "break" is the only way to skip the "else" clause
whilst still executing the code that follows the for/while loop.

So the only way to the "else" clause is useful is when a "break" can skip
it. If there is no "break", you might as well remove the "else" and

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20091008/3d61901e/attachment.html>

More information about the Python-ideas mailing list