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

Steven D'Aprano steve at pearwood.info
Fri Oct 9 04:53:12 CEST 2009

On Fri, 9 Oct 2009 03:19:35 am Stephen J. Turnbull wrote:
> Masklinn writes:
>  > Both would, because in both cases the `else:` clause serves no
>  > purpose whatsoever. It's noise.
> Tut, tut.  In Nick's proposal, both would raise because there is an
> "else:" but there is no break.  I doubt the Python compiler would add
> the editorial comment about "noise," though.<wink>

But that's exactly what it is doing, even if it doesn't use the 
word "noise". It's making an editorial judgement that perfectly legal 
code that does exactly what it is meant to do is somehow "bad" and 
requires a warning.

If I see a message SyntaxWarning('for...else with no break'), my 
immediate response is "Yes, so what?". The code runs, it runs 

People write code incrementally. A for...else block that ends up 
including a break may go through many edit-compile-run cycles before 
the break actually gets inserted. I shouldn't have to add a 
spurious "if False: break" to shut the compiler up during those 
incremental edits.

Steven D'Aprano

More information about the Python-ideas mailing list