On 16/10/2020 01:36, Chris Angelico wrote:
On Fri, Oct 16, 2020 at 10:45 AM Rob Cliffe via Python-ideas
<python-ideas@python.org> wrote:
One workaround I sometimes use is a pseudo-loop which is executed once
only; whenever the process can be abandoned, I `break` out of the
"loop". It ain't particularly elegant, but it works. Applying to the
OP's example we might have:
for _ in '1': # pseudo-loop
# NB One gotcha is to write a pseudo-loop with something like "while True"
# and then forget to put in a final `break'.
try:
something
break
except SomeError:
pass
try:
something_else
break
except AnotherError:
pass
try:
something_completely_different
except Whatever:
return NotImplemented
I sometimes wish that Python provided nicer syntax for writing a
"pseudo-loop", but I guess it's not an urgent need. And I guess the
same applies to the OP's suggestion. So I'm -0.8 on it.
You mean like.... a goto statement? I'm not sure what a "pseudo-loop"
is, other than a way to use break as goto.
ChrisA
Is that bad? As I tried to explain, sometimes a process may need to be
abandoned at multiple points instead of running to completion (it may
succeed early or fail early).
If the process were in a dedicated function, this would be done by
multiple return statements, and nobody would raise an eyebrow.
However, sometimes it is not convenient to hive the process off into its
own function (it might necessitate passing and receiving long
hard-to-maintain lists of what used to be local variables). However the
concept remains the same. Again, nobody would raise an eyebrow at
breaking out of a loop that is (potentially) executed multiple times.
That's just as much using break as a goto - in fact it leads to more
possible code paths, hence can be harder to analyse.
Sorry, but I get the impression from your comment (comparing it to goto)
that you think breaking out of a ... let's call it a once-only loop ...
is a Bad Thing and to be discouraged. If I've misunderstood, I
apologise and I'm happy. The fact remains that, in certain situations,
I have found `for _ in '1':` or equivalent (suitably documented) to be
the best way of coding that I can think of.
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/5J4EEFTKLGLSG6UULPWH6XSZLOIC4ICX/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
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/2QFCZQZB2O7KA6322A4RB5LHSNFUBKZX/
Code of Conduct: http://python.org/psf/codeofconduct/