Possible Addition to Python Language: Marked Sub-condition

MRAB python at mrabarnett.plus.com
Sun Mar 8 12:07:51 EDT 2020


On 2020-03-08 10:05, Shrinivas Kulkarni 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()
> 
An expression { ... } is already defined as a set.

> 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.
> 
In Python 3.8+ there's the "walrus operator" which lets you assign 
within an expression:

if (first := (color == BLUE and count == 20)) or (second := (color == 
RED and count % 5 == 0)):
     rotate_the_wheel()
     if first:
         set_signal()
     if second:
         unset_signal()
      proc_post_rotate()

However, this has the problem that if the first subexpression is true, 
the second will not be evaluated, so 'second' would not be set.

The simplest and clearest solution is just to write:

first = color == BLUE and count == 20
second = color == RED and count % 5 == 0
if first or second:
     rotate_the_wheel()
     if first:
         set_signal()
     if second:
         unset_signal()
      proc_post_rotate()


More information about the Python-list mailing list