PEP 315: Enhanced While Loop

Alex Martelli aleax at aleax.it
Sat May 3 05:06:04 EDT 2003


Ben Allfree wrote:

> Can you provide an example snippit?
> 
> I'm interested, but I can't come up with a scenario where the loop
> couldn't be rewritten, except for do..until loops that must execute at
> least once, which require the first loop iteration to be placed before a
> while loop.

Those "can be rewritten" too, of course.

In general, the proposed syntax:

  do:
     setup
  while condition:
     remainder

has exactly the same semantics as:

  while True:
    setup
    if not condition: break
    remainder

so the rewrite is always trivially easy.  The PEP claims the current
form is unreadable -- and that syntax-sugar issue is all that needs
to be discussed (as well as choice of new keyword -- if the new PEP
was accepted I'd rather have the more readable 'loop' instead of
the totally obscure 'do').


> And for that scenario, I would propose a simple "until" loop construct
> that is guaranteed to execute once before evaluation:
> 
> # Old way
> MakeMoney()
> while Poor():
>    MakeMoney()
> 
> # New way
> until not Poor():
>    MakeMoney()

Rewritable as:

  while True:
      MakeMoney()
      if not Poor(): break

> The above example assumes one must MakeMoney() before running the Poor()
> test ;)
> 
> Python programmers would just have to understand that the "until"
> construct executes once before evaluation. Or maybe it could be named
> something more obvious. "AfterWhile" - heh - no pun intended.

I find myself quite grateful that the designer of Python will never
allow anything as absurd as your proposal -- an unreadable "backwards"
ordering (condition appears before but is tested after) AND a new
keyword, all to "solve" *HALF* of an (arguable) problem, when it's
just as easy, as the PEP shows, to solve ALL of it WITHOUT imposing
"out of order" sequencing of source code vs execution order!


Alex





More information about the Python-list mailing list