[Python-ideas] for/else syntax

Georg Brandl g.brandl at gmx.net
Sat Oct 3 19:04:56 CEST 2009


Yuvgoog Greenle schrieb:
> Ok, in theory, it's possible to flag a boolean after a for/while when
> compiling, but I think the "special syntax" option might be simpler to
> implement. So let's talk grammar...
> 
> The old grammar was:
> 
> while_stmt ::=  "while" expression ":" suite
>                 ["else" ":" suite]
> 
> for_stmt ::=  "for" target_list "in" expression_list ":" suite
>               ["else" ":" suite]
> 
> 
> And the new one I propose looks like this:
> 
> while_stmt ::=  "while" expression ":" suite
>                 ["if" expression ":" suite]
> 
> for_stmt ::=  "for" target_list "in" expression_list ":" suite
>               ["if" expression ":" suite]
> 
> 
> The "if" expression would be evaluated regularly but would have a
> magic boolean "break" that exists only in the if statement's
> expression eval. Note a few things:
> 1. the "if" would have been evaluated either way after the loop
> (exceptions/returns aside).
> 2. the only thing that's changed is the existence of the magic boolean "break".
> 3. You don't have to use it as the old "for...else" but you can and
> it's perfectly readable.
> 
> I'm no expert on the compiler, but I have a strong feeling this is doable.

Sure, almost any language change is doable if you bend over backwards far
enough and break all rules of consistency.

Georg

-- 
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.




More information about the Python-ideas mailing list