A suggestion for a do...while loop

I'm a huge fan of the do...while loop in other languages, and it would often be useful in Python too, when doing stuff like: while True: password = input() if password == ...: break I've seen the pep 315 which got rejected, and I believe these two suggestions were mostly focused on: 1. do ... while <condition>: <body> 2. do: <body> while <condition> But both were rejected for valid reasons: 1. It makes little sense to have the while at the top, since it might need to use variables from within the body. That's the whole point of the do...while loop 2. There's no other syntax like this in Python, where you'd need a closing unindentation. Also it's using the existing "while" keyword with a different syntax than the current one What I'd like to suggest is a different approach, with an existing syntax found in functions. At first it might sound silly, but I belive it makes sense after a while: do: <body> Now bare with me a second, this is similar to function's def <header>: <body> And similar to how you can exit functions with `return`, you could also exit the `do` loop with some keyword, such as `until`. This makes it very similar to how `return` behaves in a function: do: password = input() until password == secret_password Now before you say "but return can be anywhere in the function, and there can be multiple of them", I suggest we do the same for "until". It would be like a break, but with a condition: do: password = input('Password: ') until password == secret_password # This line only gets printed if until failed print('Invalid password, try again!') print('You have successfully signed in!') The keywords are obviously a subject to change, I've also been tinkering with repeat/until, do/breakif, repeat/breakon, etc. Any thoughts, is this complete madness?

On Jun 25, 2017 07:58, "Markus Meskanen" <markusmeskanen@gmail.com> wrote: I'm a huge fan of the do...while loop in other languages, and it would often be useful in Python too, when doing stuff like: while True: password = input() if password == ...: break I've seen the pep 315 which got rejected, and I believe these two suggestions were mostly focused on: 1. do ... while <condition>: <body> 2. do: <body> while <condition> But both were rejected for valid reasons: 1. It makes little sense to have the while at the top, since it might need to use variables from within the body. That's the whole point of the do...while loop 2. There's no other syntax like this in Python, where you'd need a closing unindentation. Also it's using the existing "while" keyword with a different syntax than the current one What I'd like to suggest is a different approach, with an existing syntax found in functions. At first it might sound silly, but I belive it makes sense after a while: do: <body> Now bare with me a second, this is similar to function's def <header>: <body> And similar to how you can exit functions with `return`, you could also exit the `do` loop with some keyword, such as `until`. This makes it very similar to how `return` behaves in a function: do: password = input() until password == secret_password Now before you say "but return can be anywhere in the function, and there can be multiple of them", I suggest we do the same for "until". It would be like a break, but with a condition: do: password = input('Password: ') until password == secret_password # This line only gets printed if until failed print('Invalid password, try again!') print('You have successfully signed in!') The keywords are obviously a subject to change, I've also been tinkering with repeat/until, do/breakif, repeat/breakon, etc. Any thoughts, is this complete madness? The barrier for adding a new keyword is extremely high since anyone using the name in their code will have their code break. If we were going to do something like this, I would prefer to use existing keywords. Perhaps break if condition And we could make "while:" (with no condition) act as "while True:" But I think the benefit of either of these changes is minimal compared to what we already have.

On 25/06/2017 12:58, Markus Meskanen wrote:
I don't see any significant advantage in providing an extra Way To Do It. Granted, the "while True" idiom is an idiosyncrasy, but it is frequently used and IMHO intuitive and easy to get used to. Your suggestion doesn't even save a line of code, given that you can write: while True: password = input('Password:') if password == secret_password: break print('Invalid password, try again!') Regards Rob Cliffe

On 26 June 2017 at 10:25, Rob Cliffe <rob.cliffe@btinternet.com> wrote:
Right, this is the key challenge for do-while loops in Python: can you come up with something that's significantly clearer than the current "while True/if/break" pattern? The biggest weakness of that idiom is that it isn't really explicit in the header line - there's nothing about "while True:" that directly tells the reader "This loop is expected to exit via a break statement". If we wanted to allow that to be expressed literally, we could probably special case the "while not break" keyword sequence as a do loop: while not break: # Setup if condition: break # Loop continuation That more explicit declaration of intent ("The code in the loop body will conditionally break out of this loop") would allow a couple of things: - the compiler could warn that an else clause attached to such a loop will never execute (technically it could do that for any expression that resolves to `True` as a constant) - code linters could check the loop body for a break statement and complain if they didn't see one Logically, it's exactly the same as writing "while True:", but whereas that spelling suggests "infinite loop", the "while not break:" spelling would more directly suggest "terminated inside the loop body via a break statement" Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On Jun 26, 2017 2:15 AM, "Chris Angelico" <rosuav@gmail.com> wrote: On Mon, Jun 26, 2017 at 4:08 PM, Terry Reedy <tjreedy@udel.edu> wrote:
And for completeness, "while not throw:" All these situations could be handled by making a "while:" with no condition act as "while True:" But they could also be handled by updating pep8 to make "while True:" the recommended infinite loop syntax and make linters smarter about this (if they aren't already).

On Mon, Jun 26, 2017 at 2:41 PM, Nick Coghlan <ncoghlan@gmail.com> wrote:
What I like doing is writing these loops with a string literal as the "condition". It compiles to the same bytecode as 'while True' does, and then you can say what you like in the string. (An empty string would be like 'while False', but there's no point doing that anyway.) So, for example: while "password not correct": password = input('Password:') if password == secret_password: break print('Invalid password, try again!') ChrisA

On Jun 25, 2017 07:58, "Markus Meskanen" <markusmeskanen@gmail.com> wrote: I'm a huge fan of the do...while loop in other languages, and it would often be useful in Python too, when doing stuff like: while True: password = input() if password == ...: break I've seen the pep 315 which got rejected, and I believe these two suggestions were mostly focused on: 1. do ... while <condition>: <body> 2. do: <body> while <condition> But both were rejected for valid reasons: 1. It makes little sense to have the while at the top, since it might need to use variables from within the body. That's the whole point of the do...while loop 2. There's no other syntax like this in Python, where you'd need a closing unindentation. Also it's using the existing "while" keyword with a different syntax than the current one What I'd like to suggest is a different approach, with an existing syntax found in functions. At first it might sound silly, but I belive it makes sense after a while: do: <body> Now bare with me a second, this is similar to function's def <header>: <body> And similar to how you can exit functions with `return`, you could also exit the `do` loop with some keyword, such as `until`. This makes it very similar to how `return` behaves in a function: do: password = input() until password == secret_password Now before you say "but return can be anywhere in the function, and there can be multiple of them", I suggest we do the same for "until". It would be like a break, but with a condition: do: password = input('Password: ') until password == secret_password # This line only gets printed if until failed print('Invalid password, try again!') print('You have successfully signed in!') The keywords are obviously a subject to change, I've also been tinkering with repeat/until, do/breakif, repeat/breakon, etc. Any thoughts, is this complete madness? The barrier for adding a new keyword is extremely high since anyone using the name in their code will have their code break. If we were going to do something like this, I would prefer to use existing keywords. Perhaps break if condition And we could make "while:" (with no condition) act as "while True:" But I think the benefit of either of these changes is minimal compared to what we already have.

On 25/06/2017 12:58, Markus Meskanen wrote:
I don't see any significant advantage in providing an extra Way To Do It. Granted, the "while True" idiom is an idiosyncrasy, but it is frequently used and IMHO intuitive and easy to get used to. Your suggestion doesn't even save a line of code, given that you can write: while True: password = input('Password:') if password == secret_password: break print('Invalid password, try again!') Regards Rob Cliffe

On 26 June 2017 at 10:25, Rob Cliffe <rob.cliffe@btinternet.com> wrote:
Right, this is the key challenge for do-while loops in Python: can you come up with something that's significantly clearer than the current "while True/if/break" pattern? The biggest weakness of that idiom is that it isn't really explicit in the header line - there's nothing about "while True:" that directly tells the reader "This loop is expected to exit via a break statement". If we wanted to allow that to be expressed literally, we could probably special case the "while not break" keyword sequence as a do loop: while not break: # Setup if condition: break # Loop continuation That more explicit declaration of intent ("The code in the loop body will conditionally break out of this loop") would allow a couple of things: - the compiler could warn that an else clause attached to such a loop will never execute (technically it could do that for any expression that resolves to `True` as a constant) - code linters could check the loop body for a break statement and complain if they didn't see one Logically, it's exactly the same as writing "while True:", but whereas that spelling suggests "infinite loop", the "while not break:" spelling would more directly suggest "terminated inside the loop body via a break statement" Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On Jun 26, 2017 2:15 AM, "Chris Angelico" <rosuav@gmail.com> wrote: On Mon, Jun 26, 2017 at 4:08 PM, Terry Reedy <tjreedy@udel.edu> wrote:
And for completeness, "while not throw:" All these situations could be handled by making a "while:" with no condition act as "while True:" But they could also be handled by updating pep8 to make "while True:" the recommended infinite loop syntax and make linters smarter about this (if they aren't already).

On Mon, Jun 26, 2017 at 2:41 PM, Nick Coghlan <ncoghlan@gmail.com> wrote:
What I like doing is writing these loops with a string literal as the "condition". It compiles to the same bytecode as 'while True' does, and then you can say what you like in the string. (An empty string would be like 'while False', but there's no point doing that anyway.) So, for example: while "password not correct": password = input('Password:') if password == secret_password: break print('Invalid password, try again!') ChrisA
participants (9)
-
Chris Angelico
-
Greg Ewing
-
Joao S. O. Bueno
-
Markus Meskanen
-
Nick Coghlan
-
Rob Cliffe
-
Serhiy Storchaka
-
Terry Reedy
-
Todd