This isn't a terrible use of the walrus operator either. if blue_20 := (color==BLUE and count==20) or red_5 := (color==RED and count%5==0) 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
On Sun, Mar 8, 2020 at 12:02 PM Paul Moore <p.f.mo...@gmail.com> wrote: > 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 > _______________________________________________ > Python-ideas mailing list -- python-id...@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-id...@python.org/message/3M2BOILT7FBGGJBFECZZKDSTHRKDANCA/ > Code of Conduct: http://python.org/psf/codeofconduct/ > -- Keeping medicines from the bloodstreams of the sick; food from the bellies of the hungry; books from the hands of the uneducated; technology from the underdeveloped; and putting advocates of freedom in prisons. Intellectual property is to the 21st century what the slave trade was to the 16th. -- https://mail.python.org/mailman/listinfo/python-list