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

Gerald Britton gerald.britton at gmail.com
Fri Oct 9 15:03:14 CEST 2009

Thanks Steve.

This is what I've been trying to say all along, but you have done it
more eloquently than I.

On Thu, Oct 8, 2009 at 10:23 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Thu, 8 Oct 2009 08:24:46 pm Yuvgoog Greenle wrote:
>> In order to understand the "else" clause it's better that you think
>> of it as an "if not break" after the loop.
> That is completely wrong. for...else is a single block, and break jumps
> to the end of the block. See my previous reply for details.
> for x in seq:
>    whatever
> else:
>    suite
> is a single block. It's not the same as:
> for x in seq:
>    whatever
> suite
> even if they happen to execute the same way. In the first case, the
> entire block is one single lump of code. In the second case, you have
> two independent lumps of code, a for-loop with no else, followed by an
> independent bunch of code following it.
> Lumps of code in this sense are a weaker grouping than functions,
> classes or modules, but they are a grouping. When you see for...else
> you KNOW that the else-suite belongs with the for without needing to
> read the suite in detail. You can't pick it up and move it somewhere
> else and expect it to work, they belong together, in that order. The
> presence of the else tells the reader that the entire for...else block
> is one single conceptual unit.
> This extra information is missing in the second case, where there is
> nothing suggesting that suite belongs with the for-loop: they look
> independent. To learn that they're not, you have to read the code in
> enough detail to understand it fully.
> --
> Steven D'Aprano
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

Gerald Britton

More information about the Python-ideas mailing list