On 01/03/2022 23:57, Ben Rudiak-Gould wrote:
On Tue, Mar 1, 2022 at 2:23 PM Steven D'Aprano <[email protected]> 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
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/S3KS7KJIEXJUAVZAUJKCF5XU6QRDONEY/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to