
On 01/03/2022 23:57, Ben Rudiak-Gould wrote:
On Tue, Mar 1, 2022 at 2:23 PM Steven D'Aprano <steve@pearwood.info> wrote:
try: do_this() if condition: raise MyBreak do_that() if condition: raise MyBreak do_next_step() if condition: raise MyBreak do_last_step() except MyBreak: pass
Why not:
while True: [loop body with ordinary breaks] break
It's less to write and almost free of charge*, and it also supports redo (continue).
"Almost free" is not free. One cost is when you forget to add the final `break`. (I'm sure I've done it!) More important, it obscures the fact that the "loop" is intended to be executed only once (particularly if the loop body is long, so that the final `break` is a long way from the `while True`). `while True` normally introduces a loop that can be executed multiple times or indefinitely. Best wishes Rob cliffe