<div dir="ltr"><br><div class="gmail_quote">On Fri, Oct 2, 2009 at 8:06 AM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><br></div>
for x in xs:<br>
    if __debug__:<br>
        break<br>
else:<br>
    print "whatever"<br>
<br>
"if __debug__: suite" is special: the Python compiler optimizes the<br>
entire suite away when running with the -O flag. So if Python would<br>
treat the presence of an else as an error unless there was a break, you<br>
could have some code which was, or wasn't, legal according to the<br>
presence of the optimize flag. This is clearly a Bad Thing.<br>
<br></blockquote><div><br></div>Concerning "if __debug__: break", I'm no expert on this subject, but I'm guessing that python does it in the following order:<div>1. Parse</div><div>2. Compile to bytecode</div>
<div><br></div><div>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.</div>
<div><br></div><div>As I said, this is just an educated guess, please correct me if I'm wrong.</div><div> </div></div></div>