On Sun, 8 Mar 2020 at 15:02, Shrinivas Kulkarni <shrin...@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.
I would have thought that simply naming the sub-conditions would be sufficient. blue_20 = (color == BLUE and count == 20) red_5 = (color == RED and count % 5 == 0) if blue_20 or red_5: rotate_the_wheel() # Common to the two sub-conditions if blue_20: # First sub-condition set_signal() if red_5: # Second sub-condition clear_signal() proc_post_rotate() # Common to the two sub-conditions I don't know how experienced you are with Python programming, but if you had framed your question as "how do I modify this code to avoid repeating the conditions?" you would likely have been given this advice on the python-list mailinglist, or other similar Python programming help resources. Starting with a proposed language change before you've explored the existing options isn't likely to be the best approach (and would likely have meant you could resolve your issue without needing to bring it to python-ideas at all). Paul -- https://mail.python.org/mailman/listinfo/python-list