PEP-0315--Enhanced While Loop: An idea for an alternative syntax

Discussion in 'Python' started by Andrew Koenig, Feb 18, 2004.

  1. PEP 315 suggests that a statement such as

    do:
    x = foo()
    while x != 0:
    bar(x)

    be equivalent to

    while True:
    x = foo()
    if x == 0:
    break
    bar(x)

    I like the overall idea, but wonder about the extra keyword. Moreover, if
    you see

    x = foo()
    while x != 0:
    bar(x)

    you have to read backward to see whether the assignment to x is part of this
    statement or part of a previous statement.

    I have a proposal for an alternative syntax that solves both of these
    problems:

    while:
    x = foo()
    and while x != 0:
    bar(x)

    This example actually incorporates two changes:

    1) "while:" is equivalent to "while True:"

    2) "and while <condition>:" is an exit from the middle of the loop,
    analogously to PEP 315.

    My proposal allows for multiple exits in the obvious way

    while <condition 1>:
    <statements 1>
    and while <condition 2>:
    <statements 2>
    and while <condition 3>:
    <statements 3>
    ...

    in which all of the conditions and statements are evaluated in the order
    shown until a condition is false, at which point the loop terminates.


    It also occurs to me that this notion can be generalized to include "or
    while" as well as "and while". Using "or while" would implement a control
    structure that Dijkstra proposed in "A Discipline of Programming". The idea
    would be that

    while <condition 1>:
    <statements 1>
    or while <condition 2>:
    <statements 2>
    or while <condition 3>:
    <statements 3>

    would be equivalent to

    while True:
    if <condition 1>:
    <statements 1>
    elif <condition 2>:
    <statements 2>
    elif <condition 3>:
    <statements 3>:
    else:
    break

    This notion is still partly baked, if for no other reason than that I'm not
    sure how a statement with both "or while" and "and while" clauses should
    work. My first thought is for "and while" to have priority over "or while",
    analogously with expressions, but then the following example came to me.
    It's the inner loop of a binary search:

    while low < hi:
    mid = (low + hi) // 2;
    and while value < table[mid]:
    high = mid
    or while value > table[mid]
    low = mid

    If "and" has higher precedence than "or", this example doesn't work. So I'm
    not sure about its merits -- but I'm mentioning it in case someone else can
    improve on it.
    Andrew Koenig, Feb 18, 2004
    #1
    1. Advertising

  2. Andrew Koenig

    Paul Rubin Guest

    "Andrew Koenig" <> writes:
    > PEP 315 suggests that a statement such as
    >
    > do:
    > x = foo()
    > while x != 0:
    > bar(x)


    I'm still scratching my head over that PEP. Got a real-world code sample
    that it would improve?
    Paul Rubin, Feb 18, 2004
    #2
    1. Advertising

  3. Paul Rubin wrote:

    > I'm still scratching my head over that PEP. Got a real-world code
    > sample
    > that it would improve?


    do:
    line = inputFile.readline()
    while line:
    ...
    line = inputFile.readline()

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    \__/ The doors of Heaven and Hell are adjacent and identical.
    -- Nikos Kazantzakis
    Erik Max Francis, Feb 18, 2004
    #3
  4. Erik Max Francis wrote:

    > Paul Rubin wrote:
    >
    > > I'm still scratching my head over that PEP. Got a real-world code
    > > sample
    > > that it would improve?

    >
    > do:
    > line = inputFile.readline()
    > while line:
    > ...
    > line = inputFile.readline()


    Man, what a perfect gaffe on my part. The purpose of the do...while
    construct is to _eliminate_ the duplication. This should be:

    do:
    line = inputFile.readline()
    while line:
    ...

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    \__/ The doors of Heaven and Hell are adjacent and identical.
    -- Nikos Kazantzakis
    Erik Max Francis, Feb 18, 2004
    #4
  5. Andrew Koenig

    Paul Rubin Guest

    Erik Max Francis <> writes:
    > do:
    > line = inputFile.readline()
    > while line:
    > ...
    > line = inputFile.readline()


    So it's yet another workaround for Python's lack of assignment
    expressions. I like

    while (line := inputFile.readline()):
    ...

    a lot better.
    Paul Rubin, Feb 18, 2004
    #5
  6. Paul Rubin wrote:

    > So it's yet another workaround for Python's lack of assignment
    > expressions. I like
    >
    > while (line := inputFile.readline()):
    > ...
    >
    > a lot better.


    (Please check my correction, it's actually not as dumb as I suggested.)

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    \__/ The doors of Heaven and Hell are adjacent and identical.
    -- Nikos Kazantzakis
    Erik Max Francis, Feb 18, 2004
    #6
  7. Andrew Koenig

    Paul Rubin Guest

    Erik Max Francis <> writes:
    > Man, what a perfect gaffe on my part. The purpose of the do...while
    > construct is to _eliminate_ the duplication. This should be:
    >
    > do:
    > line = inputFile.readline()
    > while line:
    > ...


    I still like the assignment expression better.
    Paul Rubin, Feb 18, 2004
    #7
  8. Andrew Koenig

    John Roth Guest

    "Paul Rubin" <http://> wrote in message
    news:...
    > Erik Max Francis <> writes:
    > > do:
    > > line = inputFile.readline()
    > > while line:
    > > ...
    > > line = inputFile.readline()

    >
    > So it's yet another workaround for Python's lack of assignment
    > expressions. I like
    >
    > while (line := inputFile.readline()):
    > ...
    >
    > a lot better.


    It may go better in this instance, but assignment in expressions
    does nothing for multi-line initialization/reinitialization constructs.

    John Roth
    John Roth, Feb 18, 2004
    #8
  9. Andrew Koenig

    Miki Tebeka Guest

    Hell Erik,

    > do:
    > line = inputFile.readline()
    > while line:
    > ...

    Just do it with generators:
    for line in iter(inputFile.readline, ""):
    ...

    Miki
    Miki Tebeka, Feb 18, 2004
    #9
  10. "Paul Rubin" <http://> wrote in message
    news:...
    > Erik Max Francis <> writes:
    > > do:
    > > line = inputFile.readline()
    > > while line:
    > > ...
    > > line = inputFile.readline()

    >
    > So it's yet another workaround for Python's lack of assignment
    > expressions. I like
    >
    > while (line := inputFile.readline()):
    > ...
    >
    > a lot better.


    I don't, because it limits what is done before the test to a single
    expression.

    This PEP is really a generalization of the "while (cond) expr" and "do expr
    while(cond);" statements in C. C allows the loop exit to be one expression
    away from the beginning or one expression away from the end of the loop;
    this PEP would allow it anywhere.
    Andrew Koenig, Feb 18, 2004
    #10
  11. > I'm still scratching my head over that PEP. Got a real-world code sample
    > that it would improve?


    Any loop that is currently written in the form

    while True:
    <statements>
    if condition:
    break
    more statements
    Andrew Koenig, Feb 18, 2004
    #11
  12. In article <>, Miki Tebeka wrote:
    > Just do it with generators:
    > for line in iter(inputFile.readline, ""):


    Or since there's already a tp_iter slot that does that in files these
    days:

    for line in inputFile:
    #do stuff

    I'm on the side of the argument that says more interators/generators
    than 'funny' looping constructs. Most if not all of the examples I've
    seen so far could be written with a generator/iterator to good
    effect. It's higher level and clearer code; more Pythonic IMHO.

    Stefan,
    --
    Stefan Axelsson (email at http://www.cs.chalmers.se/~sax)
    Stefan Axelsson, Feb 18, 2004
    #12
  13. Andrew Koenig <> wrote:

    > while:
    > x = foo()
    > and while x != 0:
    > bar(x)


    This is the best syntax I've seen suggested so far. Tentatively +1
    (shouldn't cause any parser difficulties should it?); the lack of new
    keywords is a plus too.

    > 'or while' -


    > while True:
    > if <condition 1>:
    > <statements 1>
    > elif <condition 2>:
    > <statements 2>


    Shouldn't this logically be 'else while' then? 'or while' would imply to
    me that there was fall-through from <statements 1> to <statements 2>. I
    suppose 'else while' could cause confusion with Python's while...else
    construct though.

    Not sure this would see a lot of use; I can't think of the last time I used
    a structure like that.

    Here's another construct suggested by 'and while':

    if x is not None:
    x.connect()
    q= x.fetch()
    and if len(q)>=1:
    a= q[0]
    else:
    raise Foo() # one of the previous conditions was False

    --
    Andrew Clover
    mailto:
    http://www.doxdesk.com/
    Andrew Clover, Feb 18, 2004
    #13
  14. Andrew Koenig

    Paul Rubin Guest

    "Andrew Koenig" <> writes:
    > Any loop that is currently written in the form
    >
    > while True:
    > <statements>
    > if condition:
    > break
    > more statements


    If the loop is complicated enough, it's probably easier to just write
    it the way you've written it there. If it's uncomplicated, there's
    probably a way to do it with iterators, assignment expressions, or
    whatever. Got a real world example that really benefits from the new
    construction?
    Paul Rubin, Feb 18, 2004
    #14
  15. > while:
    > x = foo()
    > and while x != 0:
    > bar(x)
    >
    > This example actually incorporates two changes:
    >
    > 1) "while:" is equivalent to "while True:"
    >
    > 2) "and while <condition>:" is an exit from the middle of the loop,
    > analogously to PEP 315.


    How about "until" instead of "and while not", shorter and not new in the
    programing languages.

    while:
    x = foo()
    until x == 0:
    bar(x)


    --
    Дамјан (jabberID:)

    hundred-and-one symptoms of being an internet addict:
    21. Your dog has its own home page.
    =?UTF-8?B?0LTQsNC80ZjQsNC9INCzLg==?=, Feb 18, 2004
    #15
  16. On 18 Feb 2004 04:57:35 -0800, (Miki Tebeka) wrote:

    >Hell Erik,
    >
    >> do:
    >> line = inputFile.readline()
    >> while line:
    >> ...

    >Just do it with generators:
    >for line in iter(inputFile.readline, ""):
    > ...



    With read() instead of readline(), then.

    I've certainly run into this using urllib2.
    Terry Carroll, Feb 18, 2004
    #16
  17. Andrew Koenig

    Peter Hansen Guest

    Andrew Clover wrote:
    >
    > Andrew Koenig <> wrote:
    >
    > > while:
    > > x = foo()
    > > and while x != 0:
    > > bar(x)

    >
    > This is the best syntax I've seen suggested so far. Tentatively +1
    > (shouldn't cause any parser difficulties should it?); the lack of new
    > keywords is a plus too.


    So
    while:

    is a synonym for

    while 1:

    in the above, and the line

    and while x != 0:

    line is basically a synonym for

    if x == 0: break

    but with the added disadvantages of requiring a syntax change, using
    negative logic, and messing with the indentation philosophy in a
    non-intuitive way.

    Great.

    -Peter
    Peter Hansen, Feb 18, 2004
    #17
  18. Andrew Koenig

    Evan Simpson Guest

    Evan Simpson, Feb 18, 2004
    #18
  19. Andrew Koenig wrote:
    > PEP 315 suggests that a statement such as
    >
    > do:
    > x = foo()
    > while x != 0:
    > bar(x)
    >
    > be equivalent to
    >
    > while True:
    > x = foo()
    > if x == 0:
    > break
    > bar(x)
    >
    > I like the overall idea, but wonder about the extra keyword. Moreover, if
    > you see
    >
    > x = foo()
    > while x != 0:
    > bar(x)
    >
    > you have to read backward to see whether the assignment to x is part of this
    > statement or part of a previous statement.
    >
    > I have a proposal for an alternative syntax that solves both of these
    > problems:
    >
    > while:
    > x = foo()
    > and while x != 0:
    > bar(x)


    I like the "while:" and "and while:" syntax, but I think the "or while"
    is confusing. I think it's worth an addition to PEP315

    However, I remember reading Stroustrup saying he was not using the
    do-while at all (maybe in D&E, I'm not sure). Even if I have used
    do-while few times, I have the feeling it should not be added to Python
    at all, so I guess I'm in fact against PEP315, whatever the syntax. The
    reason is, if I say "do that while you're sleeping", is doesn't mean to
    me to do it at least once, even if not sleeping. It might explain why
    so much people are not using the do-while. This new syntax is
    interesting since it changes the vocabulary, but I think it's still
    unclear. The if-break method is only one line more (if the break is on
    a new line), and is pretty clear in meaning. However, there's things
    from C like += that I'm very happy to have in Python and there's still
    things I would like to have, like a ?: equivalent with a different
    syntax, but that's another story...

    Regards,

    Nicolas Fleury
    Nicolas Fleury, Feb 18, 2004
    #19
  20. "Paul Rubin" <http://> wrote in message
    news:...
    > "Andrew Koenig" <> writes:
    > > Any loop that is currently written in the form
    > >
    > > while True:
    > > <statements>
    > > if condition:
    > > break
    > > more statements

    >
    > If the loop is complicated enough, it's probably easier to just write
    > it the way you've written it there. If it's uncomplicated, there's
    > probably a way to do it with iterators, assignment expressions, or
    > whatever. Got a real world example that really benefits from the new
    > construction?


    I think that reserving "break" for abnormal situations makes programs easier
    to understand. Therefore, I would like a way of saying that I expect the
    normal exit (or exits) from the loop to be in the middle. Whether you
    consider this desire to be part of the real world is up to you.
    Andrew Koenig, Feb 18, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Remi Bastide
    Replies:
    5
    Views:
    13,548
    elizas
    Mar 29, 2010
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,217
    Tim Slattery
    Mar 30, 2005
  3. Uday Bidkar
    Replies:
    4
    Views:
    476
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  4. Daniel Cotton

    PEP 315: Enhanced While Loop

    Daniel Cotton, Jun 4, 2009, in forum: Python
    Replies:
    0
    Views:
    263
    Daniel Cotton
    Jun 4, 2009
  5. Isaac Won
    Replies:
    9
    Views:
    354
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page