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