[Python-ideas] for/else syntax

Yuvgoog Greenle ubershmekel at gmail.com
Fri Oct 2 17:12:35 CEST 2009

On Fri, Oct 2, 2009 at 8:06 AM, Steven D'Aprano <steve at pearwood.info> wrote:

> for x in xs:
>    if __debug__:
>        break
> else:
>    print "whatever"
> "if __debug__: suite" is special: the Python compiler optimizes the
> entire suite away when running with the -O flag. So if Python would
> treat the presence of an else as an error unless there was a break, you
> could have some code which was, or wasn't, legal according to the
> presence of the optimize flag. This is clearly a Bad Thing.
Concerning "if __debug__: break", I'm no expert on this subject, but I'm
guessing that python does it in the following order:1. Parse
2. Compile to bytecode

Also, I'm guessing optimizations occur at compilation and not in the parsing
stage. So the "if __debug__: break" would parse normally and for..else won't
give a syntax error. The "if __debug__" block would just be deleted at
compilation time.

As I said, this is just an educated guess, please correct me if I'm wrong.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20091002/d96f2579/attachment.html>

More information about the Python-ideas mailing list