On Dec 29, 2019, at 10:57, Abdur-Rahmaan Janhangeer <arj.pyt...@gmail.com> 
wrote:
> 
>> On Sun, Dec 29, 2019 at 10:45 PM Antoine Rozo <antoine.r...@gmail.com> 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.

_______________________________________________
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/EOTUUEQF2JDJN3BILHIIZESIIWZDQ44K/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to