Re: [Python-Dev] PEP 351 - do while
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 enter_condition, exit_condition: body This would be a do-loop. while 1, exit_condition: body In situations where you want to enter a loop on one condition and exit on a second condition: if value1: value2 = True while value2: body Would be ... while value1, value2: body I've used that pattern on more than a few occasions. A single condition while would be the same as... while condition, condition:# same entry and exit condition body So do just as we do now... while condition: # same entry and exit condition body 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 ___ 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/archive%40mail-archive.com
Re: [Python-Dev] PEP 351 - do while
On 10/1/06, Ron Adam [EMAIL PROTECTED] wrote: (I don't think this has been suggested yet.) while enter_condition, exit_condition: 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 ___ 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/archive%40mail-archive.com
Re: [Python-Dev] PEP 351 - do while
(I don't think this has been suggested yet.) while enter_condition, exit_condition: 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 exit_condition is evaluated out of sequence. ___ 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/archive%40mail-archive.com
Re: [Python-Dev] PEP 351 - do while
At 12:58 PM 10/1/2006 -0400, Andrew Koenig wrote: (I don't think this has been suggested yet.) while enter_condition, exit_condition: 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 exit_condition 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. :) ___ 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/archive%40mail-archive.com
Re: [Python-Dev] PEP 351 - do while
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. ___ 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/archive%40mail-archive.com
Re: [Python-Dev] PEP 351 - do while
Michael Urman wrote: On 10/1/06, Ron Adam [EMAIL PROTECTED] wrote: (I don't think this has been suggested yet.) while enter_condition, exit_condition: 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 ___ 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/archive%40mail-archive.com
[Python-Dev] 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 Ill 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. ___ 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/archive%40mail-archive.com
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 | [EMAIL PROTECTED] | Brisbane, Australia --- http://www.boredomandlaziness.org ___ 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/archive%40mail-archive.com