PEP 315: Enhanced While Loop
Ben Allfree
benles at bldigital.com
Sat May 3 00:23:25 EDT 2003
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.
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()
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.
> -----Original Message-----
> From: python-list-admin at python.org [mailto:python-list-admin at python.org]
> On Behalf Of W Isaac Carroll
> Sent: Friday, May 02, 2003 8:47 PM
> To: Python List
> Subject: PEP 315: Enhanced While Loop
>
> PEP: 315
> Title: Enhanced While Loop
> Version: $Revision: 1.1 $
> Last-Modified: $Date: 2003/05/02 22:53:32 $
> Author: W Isaac Carroll <icarroll at pobox.com>
> Status: Draft
> Type: Standards Track
> Content-Type: text/plain
> Created: 25-Apr-2003
> Python-Version: 2.4
> Post-History:
>
>
> Abstract
>
> This PEP proposes adding an optional "do" clause to the beginning
> of the while loop to make loop code clearer and reduce errors
> caused by code duplication.
>
>
> Motivation
>
> It is often necessary for some code to be executed before each
> evaluation of the while loop condition. This code is often
> duplicated outside the loop, as setup code that executes once
> before entering the loop:
>
> <setup code>
> while <condition>:
> <loop body>
> <setup code>
>
> The problem is that duplicated code can be a source of errors if
> one instance is changed but the other is not. Also, the purpose
> of the second instance of the setup code is not clear because it
> comes at the end of the loop.
>
> It is possible to prevent code duplication by moving the loop
> condition into a helper function, or an if statement in the loop
> body. However, separating the loop condition from the while
> keyword makes the behavior of the loop less clear:
>
> def helper(args):
> <setup code>
> return <condition>
>
> while helper(args):
> <loop body>
>
> This last form has the additional drawback of requiring the loop's
> else clause to be added to the body of the if statement, further
> obscuring the loop's behavior:
>
> while True:
> <setup code>
> if not <condition>: break
> <loop body>
>
> This PEP proposes to solve these problems by adding an optional
> clause to the while loop, which allows the setup code to be
> expressed in a natural way:
>
> do:
> <setup code>
> while <condition>:
> <loop body>
>
> This keeps the loop condition with the while keyword where it
> belongs, and does not require code to be duplicated.
>
>
> Syntax
>
> The syntax of the while statement
>
> while_stmt : "while" expression ":" suite
> ["else" ":" suite]
>
> is extended as follows:
>
> while_stmt : ["do" ":" suite]
> "while" expression ":" suite
> ["else" ":" suite]
>
>
> Semantics of break and continue
>
> In the do-while loop the break statement will behave the same as
> in the standard while loop: It will immediately terminate the loop
> without evaluating the loop condition or executing the else
> clause.
>
> A continue statement in the do-while loop will behave somewhat
> differently than in the standard while loop. Instead of jumping
> back to the loop condition, it will jump to the beginning of the
> first suite of the loop. This is to ensure that the setup code
> has a chance to do its job before the condition is evaluated.
>
>
> Future Statement
>
> Because of the new keyword "do", the statement
>
> from __future__ import do_while
>
> will initially be required to use the do-while form.
>
>
> Implementation
>
> The first implementation of this PEP can compile the do-while loop
> as an infinite loop with a test that exits the loop.
>
>
> Copyright
>
> This document is placed in the public domain.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list