[Python-ideas] for/else syntax

Steven D'Aprano steve at pearwood.info
Fri Oct 2 16:35:50 CEST 2009

On Fri, 2 Oct 2009 09:01:20 pm Nick Coghlan wrote:
> Yuvgoog Greenle wrote:
> > 1. Not allowing a for loop that has no "break" to have an "else".
> > Just like "else" isn't allowed when there isn't an "except" in the
> > "try". There really is no excuse, justification or sense in a "for"
> > loop that has no "break" yet has an "else".
> As Stephen said, that's a very good point. Having the compiler issue
> a SyntaxWarning when compiling such code is perfectly possible and
> would make a great deal of sense (and probably prevent many cases of
> naive misuse).

I disagree that it makes sense. Why raise a SyntaxWarning for legal 
code? Shall we raise SyntaxWarning for the following as well?


if False:

for x in []:

except Exception:

etc. There are all sorts of code which is legal but does nothing. Why 
should the language single out one of them for a warning? If that 
warning is anywhere, it should be in PyLint or equivalent.

It's even more problematic when you consider code where the break is 
inside a "if __debug__" clause. Then you've code which raises a warning 
when you run with the -O flag, and doesn't when you run without.

Steven D'Aprano

More information about the Python-ideas mailing list