PEP 351 - do while
Hi, Just an opinion, but many uses of the 'while true loop' are instances of a 'do loop'. I appreciate the language layout question, so I'll give you an alternative: do: <body> <setup code> while <condition> Cheers, Hans Polak. This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message.
Hans Polak wrote:
Hi,
Just an opinion, but many uses of the ‘while true loop’ are instances of a ‘do loop’. I appreciate the language layout question, so I’ll give you an alternative:
do:
<body>
<setup code>
while <condition>
I believe you meant to write PEP 315 in the subject line :) To fully account for loop else clauses, this suggestion would probably need to be modified to look something like this: Basic while loop: <setup code> while <condition>: <loop body> <setup code> else: <loop completion code> Using break to avoid code duplication: while True: <setup code> if not <condition>: <loop completion code> break <loop body> Current version of PEP 315: do: <setup code> while <condition>: <loop body> else: <loop completion code> This suggestion: do: <setup code> while <condition> <loop body> else: <loop completion code> I personally like that style, and if the compiler can dig through a function looking for yield statements to identify generators, it should be able to dig through a do-loop looking for the termination condition. As I recall, the main objection to this style was that it could hide the loop termination condition, but that isn't actually mentioned in the PEP (and in the typical do-while case, the loop condition will still be clearly visible at the end of the loop body). Regards, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org
Nick Coghlan wrote:
Hans Polak wrote:
Hi,
Just an opinion, but many uses of the ‘while true loop’ are instances of a ‘do loop’. I appreciate the language layout question, so I’ll give you an alternative:
do:
<body>
<setup code>
while <condition>
(I don't think this has been suggested yet.)
while
As I recall, the main objection to this style was that it could hide the loop termination condition, but that isn't actually mentioned in the PEP (and in the typical do-while case, the loop condition will still be clearly visible at the end of the loop body).
Putting both the entry and exit conditions at the top is easier to read. The end of the first loop is also the beginning of all the following loops, so having the exit_condition at the top doesn't really put anything out of order. If the exit_condition is not evaluated until the top of the second loop, the names it uses do not need to be pre defined, they can just be assigned in the loop. Ron
On 10/1/06, Ron Adam
(I don't think this has been suggested yet.)
while
, : <body>
[snip]
Putting both the entry and exit conditions at the top is easier to read.
I agree in principle, but I thought the proposed syntax already has meaning today (as it turns out, parentheses are required to make a tuple in a while condition, at least in 2.4 and 2.5). To help stave off similar confusion I'd rather see a pseudo-keyword added. However my first candidate "until" seems to apply a negation to the exit condition. while True until False: # run once? run forever? while True until True: # run forever? run once? It's still very different from any syntactical syntax I can think of in python. I'm not sure I like the idea. Michael -- Michael Urman http://www.tortall.net/mu/blog
Michael Urman wrote:
On 10/1/06, Ron Adam
wrote: (I don't think this has been suggested yet.)
while
, : <body> [snip]
Putting both the entry and exit conditions at the top is easier to read.
I agree in principle, but I thought the proposed syntax already has meaning today (as it turns out, parentheses are required to make a tuple in a while condition, at least in 2.4 and 2.5). To help stave off similar confusion I'd rather see a pseudo-keyword added. However my first candidate "until" seems to apply a negation to the exit condition.
while True until False: # run once? run forever? while True until True: # run forever? run once?
It's still very different from any syntactical syntax I can think of in python. I'm not sure I like the idea.
Michael
I thought the comma might be a sticking point. My first thought was to have a series of conditions evaluated on loops with the last condition repeated. while loop1_cond, loop2_cond, loop3_cond, ..., rest_condition: <body> But I couldn't think of good uses past the first two that are obvious so I trimmed it down to just enter_condition and exit_condition which keeps it simple. But from this example you can see they are all really just top of the loop tests done in sequence. A do loop is just a matter of having the first one evaluate as True. The current while condition is an entry condition the first time it's evaluated and an exit condition on the rest. So by splitting it in two, we can specify an enter and exit test more explicitly. There's a certain consistency I like about this also. Is it just getting around or finding a nice alternative to the comma that is the biggest problem with this? Maybe just using "then" would work? while cond1 then cond2: <body> Cheers, Ron
(I don't think this has been suggested yet.)
while
, : <body>
This usage makes me uneasy, not the least because I don't understand why the
comma isn't creating a tuple. That is, why whould
while x, y:
<body>
be any different from
while (x, y):
<body>
?
My other concern is that
At 12:58 PM 10/1/2006 -0400, Andrew Koenig wrote:
(I don't think this has been suggested yet.)
while
, : <body> This usage makes me uneasy, not the least because I don't understand why the comma isn't creating a tuple. That is, why whould
while x, y: <body>
be any different from
while (x, y): <body>
?
My other concern is that
is evaluated out of sequence.
This pattern: while entry_cond: ... and while not exit_cond: ... has been suggested before, and I believe that at least one of the times it was suggested, it had some support from Guido. Essentially, the "and while not exit" is equivalent to an "if exit: break" that's more visible due to not being indented. I'm not sure I like it, myself, but out of all the things that get suggested for this issue, I think it's the best. The fact that it's still not very good despite being the best, is probably the reason we don't have it yet. :)
This pattern:
while entry_cond: ... and while not exit_cond: ...
has been suggested before, and I believe that at least one of the times it was suggested, it had some support from Guido. Essentially, the "and while not exit" is equivalent to an "if exit: break" that's more visible due to not being indented.
I like this suggestion. In fact it is possible that at one time I suggested something similar. It reminds me of something that Dijkstra suggested in his 1971 book "A Discipline of Programming." His ides looked somewhat like this: do condition 1 -> action 1 ... [] condition n -> action n od Here, the [] should be thought of as a delimiter; it was typeset as a tall narrow rectangle. The semantics are as follows: If all of the conditions are false, the statement does nothing. Otherwise, the implementation picks one of the true conditions, executes the corresponding action, and does it all again. There is no guarantee about which action is executed if more than one of the conditions is true. The general idea, then, is that each action should falsify its corresponding condition while bring the loop closer to termination; when all of the conditions are false, the loop is done. For example, he might write Euclid's algorithm this way: do x < y -> y := y mod x [] y < x -> x := x mod y od If we were to adopt "while ... and while" in Python, then Dijkstra's construct could be rendered this way: while x < y: y %= x or while y < x: x %= y I'm not suggesting this seriously as I don't have enough realistic use cases. Still, it's interesting to see that someone else has grappled with a similar problem.
Hi Nick, Yep, PEP 315. Sorry about that. Now, about your suggestion do: <setup code> while <condition> <loop body> else: <loop completion code> This is pythonic, but not logical. The 'do' will execute at least once, so the else clause is not needed, nor is the <loop completion code>. The <loop body> should go before the while terminator. I'm bound to reiterate my proposal: <before loop code> do: <loop body> <setup code> while <condition> <after loop code> Example (if you know there will be at least one val). source.open() do: val = source.read(1) process(val) while val != lastitem source.close() The c syntax is: do { block of code } while (condition is satisfied); The VB syntax is: do block loop while <cond> Cheers & thanks for your reply, Hans Polak. -----Original Message----- From: Nick Coghlan [mailto:ncoghlan@gmail.com] Sent: domingo, 01 de octubre de 2006 6:18 To: Hans Polak Cc: python-dev@python.org Subject: Re: [Python-Dev] PEP 351 - do while Hans Polak wrote:
Hi,
Just an opinion, but many uses of the 'while true loop' are instances of a 'do loop'. I appreciate the language layout question, so I'll give you an alternative:
do:
<body>
<setup code>
while <condition>
I believe you meant to write PEP 315 in the subject line :) To fully account for loop else clauses, this suggestion would probably need to be modified to look something like this: Basic while loop: <setup code> while <condition>: <loop body> <setup code> else: <loop completion code> Using break to avoid code duplication: while True: <setup code> if not <condition>: <loop completion code> break <loop body> Current version of PEP 315: do: <setup code> while <condition>: <loop body> else: <loop completion code> This suggestion: do: <setup code> while <condition> <loop body> else: <loop completion code> I personally like that style, and if the compiler can dig through a function looking for yield statements to identify generators, it should be able to dig through a do-loop looking for the termination condition. As I recall, the main objection to this style was that it could hide the loop termination condition, but that isn't actually mentioned in the PEP (and in the typical do-while case, the loop condition will still be clearly visible at the end of the loop body). Regards, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message.
Hans Polak wrote:
Hi Nick,
Yep, PEP 315. Sorry about that.
Now, about your suggestion do: <setup code> while <condition> <loop body> else: <loop completion code>
This is pythonic, but not logical. The 'do' will execute at least once, so the else clause is not needed, nor is the <loop completion code>. The <loop body> should go before the while terminator.
This objection is based on a misunderstanding of what the else clause is for in a Python loop. The else clause is only executed if the loop terminated naturally (the exit condition became false) rather than being explicitly terminated using a break statement. This behaviour is most commonly useful when using a for loop to search through an iterable (breaking when the object is found, and using the else clause to handle the 'not found' case), but it is also defined for while loops. Regards, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org
Ok, I see your point. Really, I've read more about Python than worked with it, so I'm out of my league here. Can I combine your suggestion with mine and come up with the following: do: <setup code> <loop body> while <condition> else: <loop completion code> Cheers, Hans. -----Original Message----- From: Nick Coghlan [mailto:ncoghlan@gmail.com] Sent: lunes, 02 de octubre de 2006 12:48 To: Hans Polak Cc: python-dev@python.org Subject: Re: [Python-Dev] PEP 315 - do while Hans Polak wrote:
Hi Nick,
Yep, PEP 315. Sorry about that.
Now, about your suggestion do: <setup code> while <condition> <loop body> else: <loop completion code>
This is pythonic, but not logical. The 'do' will execute at least once, so the else clause is not needed, nor is the <loop completion code>. The <loop body> should go before the while terminator.
This objection is based on a misunderstanding of what the else clause is for in a Python loop. The else clause is only executed if the loop terminated naturally (the exit condition became false) rather than being explicitly terminated using a break statement. This behaviour is most commonly useful when using a for loop to search through an iterable (breaking when the object is found, and using the else clause to handle the 'not found' case), but it is also defined for while loops. Regards, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message.
Hans Polak wrote:
Ok, I see your point. Really, I've read more about Python than worked with it, so I'm out of my league here.
Can I combine your suggestion with mine and come up with the following:
do: <setup code> <loop body> while <condition> else: <loop completion code>
In my example, the 3 sections (<setup code>, <loop body> and <loop completion code> are all optional. A basic do-while loop would look like this: do: <setup code> while <condition> (That is, <setup code> is still repeated each time around the loop - it's called that because it is run before the loop evaluated condition is evaluated) Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org
Nick Coghlan wrote:
Hans Polak wrote:
Ok, I see your point. Really, I've read more about Python than worked with it, so I'm out of my league here.
Can I combine your suggestion with mine and come up with the following:
do: <setup code> <loop body> while <condition> else: <loop completion code>
In my example, the 3 sections (<setup code>, <loop body> and <loop completion code> are all optional. A basic do-while loop would look like this:
do: <setup code> while <condition>
(That is, <setup code> is still repeated each time around the loop - it's called that because it is run before the loop evaluated condition is evaluated)
+1 This looks good. The current idiom works fine, but looks unnatural : while True: if <condition>: break Would a 'while' outside of a 'do' block (but without the colon) then be a syntax error ? 'do:' would just be syntactic sugar for 'while True:' I guess. Michael Foord http://www.voidspace.org.uk
Cheers, Nick.
Thanks for your reply Nick, and your support Michael. I'll leave the PEP talk to you guys :) Cheers, Hans -----Original Message----- From: Michael Foord [mailto:fuzzyman@gmail.com] On Behalf Of Fuzzyman Sent: martes, 03 de octubre de 2006 12:00 To: Nick Coghlan Cc: Hans Polak; python-dev@python.org Subject: Re: [Python-Dev] PEP 315 - do while Nick Coghlan wrote:
Hans Polak wrote:
Ok, I see your point. Really, I've read more about Python than worked with it, so I'm out of my league here.
Can I combine your suggestion with mine and come up with the following:
do: <setup code> <loop body> while <condition> else: <loop completion code>
In my example, the 3 sections (<setup code>, <loop body> and <loop completion code> are all optional. A basic do-while loop would look like this:
do: <setup code> while <condition>
(That is, <setup code> is still repeated each time around the loop - it's called that because it is run before the loop evaluated condition is evaluated)
+1 This looks good. The current idiom works fine, but looks unnatural : while True: if <condition>: break Would a 'while' outside of a 'do' block (but without the colon) then be a syntax error ? 'do:' would just be syntactic sugar for 'while True:' I guess. Michael Foord http://www.voidspace.org.uk
Cheers, Nick.
This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message.
Fuzzyman wrote:
Nick Coghlan wrote:
In my example, the 3 sections (<setup code>, <loop body> and <loop completion code> are all optional. A basic do-while loop would look like this:
do: <setup code> while <condition>
(That is, <setup code> is still repeated each time around the loop - it's called that because it is run before the loop evaluated condition is evaluated)
+1
This looks good.
I'm pretty sure it was proposed by someone else a long time ago - I was surprised to find it wasn't mentioned in PEP 315. That said, Guido's observation on PEP 315 from earlier this year holds for me too: "I kind of like it but it doesn't strike me as super important" [1]
The current idiom works fine, but looks unnatural :
while True: if <condition>: break
There's the rationale for the PEP in a whole 5 lines counting whitespace ;)
Would a 'while' outside of a 'do' block (but without the colon) then be a syntax error ?
'do:' would just be syntactic sugar for 'while True:' I guess.
That's the slight issue I still have with the idea - you could end up with multiple ways of spelling some of the basic loop forms, such as these 3 flavours of infinite loop: do: pass # Is there an implicit 'while True' at the end of the loop body? do: while True while True: pass The other issue I have is that I'm not yet 100% certain it's implementable with Python's parser and grammar. I *think* changing the definition of the while statement from: while_stmt ::= "while" expression ":" suite ["else" ":" suite] to while_stmt ::= "while" expression [":" suite ["else" ":" suite]] And adding a new AST node and a new type of compiler frame block "DO_LOOP" would do the trick (the compilation of a while statement without a trailing colon would then check that it was in a DO_LOOP block and raise an error if not). Cheers, Nick. [1] http://mail.python.org/pipermail/python-dev/2006-February/060711.html -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org
I'm against infinite loops -something religious :), which explains the call for the do loop. The issue about the parser is over my head, but the thought had occurred to me. Now, it would not affect while loops inside do loops, wouldn't it? Cheers, Hans. -----Original Message----- From: Nick Coghlan [mailto:ncoghlan@gmail.com] Sent: martes, 03 de octubre de 2006 15:51 To: Fuzzyman Cc: Hans Polak; python-dev@python.org Subject: Re: [Python-Dev] PEP 315 - do while Fuzzyman wrote:
Nick Coghlan wrote:
In my example, the 3 sections (<setup code>, <loop body> and <loop completion code> are all optional. A basic do-while loop would look like this:
do: <setup code> while <condition>
(That is, <setup code> is still repeated each time around the loop - it's
called that because it is run before the loop evaluated condition is evaluated)
+1
This looks good.
I'm pretty sure it was proposed by someone else a long time ago - I was surprised to find it wasn't mentioned in PEP 315. That said, Guido's observation on PEP 315 from earlier this year holds for me too: "I kind of like it but it doesn't strike me as super important" [1]
The current idiom works fine, but looks unnatural :
while True: if <condition>: break
There's the rationale for the PEP in a whole 5 lines counting whitespace ;)
Would a 'while' outside of a 'do' block (but without the colon) then be a syntax error ?
'do:' would just be syntactic sugar for 'while True:' I guess.
That's the slight issue I still have with the idea - you could end up with multiple ways of spelling some of the basic loop forms, such as these 3 flavours of infinite loop: do: pass # Is there an implicit 'while True' at the end of the loop body? do: while True while True: pass The other issue I have is that I'm not yet 100% certain it's implementable with Python's parser and grammar. I *think* changing the definition of the while statement from: while_stmt ::= "while" expression ":" suite ["else" ":" suite] to while_stmt ::= "while" expression [":" suite ["else" ":" suite]] And adding a new AST node and a new type of compiler frame block "DO_LOOP" would do the trick (the compilation of a while statement without a trailing colon would then check that it was in a DO_LOOP block and raise an error if not). Cheers, Nick. [1] http://mail.python.org/pipermail/python-dev/2006-February/060711.html -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message.
Nick Coghlan wrote:
[snip..]
The current idiom works fine, but looks unnatural :
while True: if <condition>: break
There's the rationale for the PEP in a whole 5 lines counting whitespace ;)
Would a 'while' outside of a 'do' block (but without the colon) then be a syntax error ?
'do:' would just be syntactic sugar for 'while True:' I guess.
That's the slight issue I still have with the idea - you could end up with multiple ways of spelling some of the basic loop forms, such as these 3 flavours of infinite loop:
do: pass # Is there an implicit 'while True' at the end of the loop body?
do: while True
while True: pass
Following the current idiom, isn't it more natural to repeat the loop 'until' a condition is met. If we introduced two new keywords, it would avoid ambiguity in the use of 'while'. do: <loop body> until <condition> A do loop could require an 'until', meaning 'do' is not *just* a replacement for an infinite loop. (Assuming the parser can be coerced into co-operation.) It is obviously still a new construct in terms of Python syntax (not requiring a colon after '<condition>'.) I'm sure this has been suggested, but wonder if it has already been ruled out. An 'else' block could then retain its current meaning (execute if the loop is not terminated early by an explicit break.) Michael Foord http://www.voidspace.org.uk
Please note that until <==> while not. do: <code> until count > 10 do: <code> while count <= 10 Cheers, Hans. -----Original Message----- From: Michael Foord [mailto:fuzzyman@gmail.com] On Behalf Of Fuzzyman Sent: martes, 03 de octubre de 2006 16:29 To: Nick Coghlan Cc: Hans Polak; python-dev@python.org Subject: Re: [Python-Dev] PEP 315 - do while Nick Coghlan wrote:
[snip..]
The current idiom works fine, but looks unnatural :
while True: if <condition>: break
There's the rationale for the PEP in a whole 5 lines counting whitespace ;)
Would a 'while' outside of a 'do' block (but without the colon) then be a syntax error ?
'do:' would just be syntactic sugar for 'while True:' I guess.
That's the slight issue I still have with the idea - you could end up with multiple ways of spelling some of the basic loop forms, such as these 3 flavours of infinite loop:
do: pass # Is there an implicit 'while True' at the end of the loop body?
do: while True
while True: pass
Following the current idiom, isn't it more natural to repeat the loop 'until' a condition is met. If we introduced two new keywords, it would avoid ambiguity in the use of 'while'. do: <loop body> until <condition> A do loop could require an 'until', meaning 'do' is not *just* a replacement for an infinite loop. (Assuming the parser can be coerced into co-operation.) It is obviously still a new construct in terms of Python syntax (not requiring a colon after '<condition>'.) I'm sure this has been suggested, but wonder if it has already been ruled out. An 'else' block could then retain its current meaning (execute if the loop is not terminated early by an explicit break.) Michael Foord http://www.voidspace.org.uk This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message.
You are all wasting your time on this. It won't go in. -- --Guido van Rossum (home page: http://www.python.org/~guido/)
On 10/4/06, Guido van Rossum
You are all wasting your time on this. It won't go in.
+1 from me. Should you mark PEP 315 as rejected? Jeremy
-- --Guido van Rossum (home page: http://www.python.org/~guido/) _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/jeremy%40alum.mit.edu
Nick Coghlan wrote:
Fuzzyman wrote:
Nick Coghlan wrote:
In my example, the 3 sections (<setup code>, <loop body> and <loop completion code> are all optional. A basic do-while loop would look like this:
do: <setup code> while <condition>
(That is, <setup code> is still repeated each time around the loop - it's called that because it is run before the loop evaluated condition is evaluated)
+1
This looks good.
I'm pretty sure it was proposed by someone else a long time ago - I was surprised to find it wasn't mentioned in PEP 315.
That said, Guido's observation on PEP 315 from earlier this year holds for me too:
"I kind of like it but it doesn't strike me as super important" [1]
I looked though a few files in the library for different while usage patterns and there really wasn't as many while loops that would fit this pattern as I expected. There are much more while loops with one or more exit conditions in the middle as things in the loop are calculated or received. So it might be smart to find out just how many places in the library it would make a difference. Ron
participants (9)
-
Andrew Koenig
-
Fuzzyman
-
Guido van Rossum
-
Hans Polak
-
Jeremy Hylton
-
Michael Urman
-
Nick Coghlan
-
Phillip J. Eby
-
Ron Adam