On 12/29/19 3:13 PM, Andrew Barnert via Python-ideas wrote:
On Dec 29, 2019, at 10:57, Abdur-Rahmaan Janhangeer
<[email protected]> wrote:
On Sun, Dec 29, 2019 at 10:45 PM Antoine Rozo <[email protected]
<mailto:[email protected]>> wrote:
If I see a "when" keyword in a code, I will think that
the body will be executed later, when the expression will be true.
Do you see an advantage having in such a case?
There are lots of advantages.
Imagine being able to do debugger-style watchpoints from within your
code without having to run in a debugger. Whenever x becomes 5 on your
live webserver, it logs a stack trace.
You could also use this to validate inputs in a GUI—people already do
this today with Tkinter, which provides variables that can be hooked
with callback functions, but syntactic support would eliminate a lot
of the boilerplate.
You could even use it to add thread safety to single-threaded
libraries from outside (when this becomes true, notify a condition
variable…). I think I wrote a facetious post on my old Stupid Python
Ideas blog on all the control flow statements Python is “missing” and
included a when for this purpose.
I don’t think the advantage is anywhere near worth the cost. Being
explicit about which variables are hooked, and where hooks are
registered, as in Tkinter, aids readability a lot more than the extra
boilerplate hurts it, and also allows the interpreter to be simpler
and faster.
But that’s exactly why the question is never “could this be useful?”
but “would this be useful enough to be worth the cost?”. And the same
question comes up for your proposal. Sure, there are cases where when
reads better than if. But that’s a small advantage. And the cost is
that we have to implement it, we break backward compatibility, and
everyone who reads Python code now has to keep 6 control flow
statements in their head instead of 5.
AH, like the old conditional COME FROM statement that was proposed for
FORTRAN decades ago.
The anti-pattern suggested to get away from structured code.
--
Richard Damon
_______________________________________________
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/PBPFIHHFE5RBB63I46QH5ZHZU7OI7GKX/
Code of Conduct: http://python.org/psf/codeofconduct/