[Python-ideas] Updating PEP 315: do-while loops

Ron Adam rrr at ronadam.com
Sun Apr 26 06:44:16 CEST 2009

Larry Hastings wrote:
> Ron Adam wrote:
>> I'd prefer a bare "while:" with a more visible "break if" and possibly 
>> "continue if" expressions.
> I do like "break if" and "continue if", unsurprisingly as I suggested 
> them in parallel.  I'm not sure about the "while:"; your "while: ... 
> continue if ; break" strikes down some of the clarity we're trying to 
> achieve here.

The bare "while:" can be a completely separate issue.


     while True:
         continue if condition

I'm not sure why you think that is less or not clear.  It's just an 
possible to do if you add 'continue if'.  <shrug>

The equivalent would be:

     while True:
         break if (not condition)

But this reverses the test and that could express what you are doing in a 
less clear way.  Being able to do it both ways is a good thing I think.

If you allow the if-else syntax to be used with flow control keywords then 
you have 6 possibilities.

       break if condition else pass
       break if condition else continue

       continue if condition else pass
       continue if condition else break

       pass if condition else break
       pass if condition else continue

It makes sense to shorten some of these..

       break if condition      # 'else pass' doesn't do anything
       continue if condition   # ""

I'm not sure the others are needed, they may allow some problems to be 
expressed more explicitly.

The nice thing about extending python this way is these expressions may 
also work in for loops and possibly other places.

Because python is based as much on practical need rather than just what is 
possible to do.  We may only need the two last shorter versions above.

But I won't complain if all six of the longer versions above also work. ;-)

In anycase Raymond was asking for ideas that are consistent with pythons 
syntax and I think these suggestions are.  The one inconsistant thing is 
if-else expressions normally return a value, and in these cases, they 
either should raise a syntax error or return None if they are used on the 
right hand side of an expression.


More information about the Python-ideas mailing list