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

geremy condra debatem1 at gmail.com
Fri Oct 9 06:00:57 CEST 2009

On Thu, Oct 8, 2009 at 10:53 PM, Steven D'Aprano <steve at pearwood.info>wrote:

> 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.

The fact that code is currently legal (or illegal) does not stop us
from making judgments about whether it should or should not be
legal- see the current discussion about relaxing the restriction
on decorators if you want an example. The only difference here
is that instead of attempting to outright forbid that behavior, the
proposal is to issue a warning notifying the programmer that
they have probably screwed up, like pretty much everybody
providing code examples in the course of this discussion.

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

You're switching definitions of "what it is meant to do" from
one based on the idea that the compiler is always right to
one based on the idea that there's some higher definition of

> 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.

So what? Suppress the warning if you want, otherwise, listen
to it, it's telling you that your code probably isn't doing what
you intended it to do. That's the sort of thing I'd generally
like to know while in the middle of the dev cycle.

Geremy Condra
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20091009/5e26e7a4/attachment.html>

More information about the Python-ideas mailing list