Hi Rajeev and others
Thanks for your responses. I completely overlooked the simple fact
that the conditions are evaluated to boolean values and as such can be
stored in variables.
In the hindsight there may perhaps be a couple of (far-fetched) merits
to my proposal (like useful in conditions with dynamic function calls
or sparing users the effort of maintaining rather ephemeral variables)
but they are of course not worth the effort of making a change to the
language itself.
So I am sorry for the spam. Will be careful the next time.
Best regards
Shrinivas Kulkarni
On Mon, Mar 9, 2020 at 10:21 AM Rajeev J Sebastian
<rajeev.sebastian@alokin.in> wrote:
>
> Hi Shrinivas,
>
> Wouldn’t these just be variables?
>
> Cond1 = color == Blue and count == 20
> Cond2 = color == Red
>
> if cond1 or cond2:
> Do something
> elif cond3:
> Do something else
>
> Regards
> Rajeev J Sebastian
> On 8 Mar 2020, 20:42 +0530, Shrinivas Kulkarni <shrinivk@gmail.com>, wrote:
>
> Hello Everyone
>
> While writing python code, I frequently come across the need to do
> certain tasks based on combined conditions.
>
> Much of the task for all the sub-conditions are common but some are
> specific to one or more of these sub-conditions.
>
> A simplified example:
>
> ########################## Code ##########################
> if (color == BLUE and count == 20) or (color == RED and count % 5 == 0):
> rotate_the_wheel() # Common to the two sub-conditions
> if(color == BLUE and count == 20): # First sub-condition
> set_signal()
> if(color == RED and count % 5 == 0): # Second sub-condition
> clear_signal()
> proc_post_rotate() # Common to the two sub-conditions
>
> I am not sure if there is a better way to do this. If not, maybe there
> can be an extension to the language, which would allow marking a
> sub-condition just like we mark a sub-expression in a regular
> expression.
>
> Tentative syntax for this could be ({} marks the sub-condition and
> \number refers back to it):
>
> ########################## Code ##########################
> if {(color == BLUE and count == 20)} or {(color == RED and count % 5 == 0)}:
> rotate_the_wheel()
> if(\1): # First marked condition
> set_signal()
> if(\2): # Second marked condition
> unset_signal()
> proc_post_rotate()
>
> And like sub-expressions, the nesting of marked sub-condions should
> also be possible:
>
> ########################## Code ##########################
> if {{(color == BLUE and count == 20)} and {value == 20}} or {(color ==
> RED and count % 5 == 0)}:
> if(\1):# Refers to the entire subcondition {{(color == BLUE and
> count == 20)} and {value = 20}}
> proc1()
> if(\2):# Refers to sub-subcondition {value == 20}
>
> This will not only avoid the repetition of sub-conditions, but make
> code readable since something like \1 will give an immediate
> indication of a sub-condition that's defined earlier.
>
> Please let me know something similar is already implemented.
> Even otherwise, all your thoughts, inputs and criticism are welcome.
>
> Thanks and best regards
> Shrinivas Kulkarni
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-leave@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/M5OUL4RC4N4LNVRHFAWB75QXNLN7G7TP/
> Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/7I3K725PUUDNIY4NDSLQGTRDTR32JQVV/
Code of Conduct: http://python.org/psf/codeofconduct/