Cleaning up conditionals

Erik python at
Fri Dec 30 18:22:02 EST 2016

On 30/12/16 23:00, Deborah Swanson wrote:
> Oops, indentation was messed up when I copied it into the email. Should
> be this:
> 		if len(l1[st]) == 0:
>                 if len(l2[st]) > 0:
>                     l1[st] = l2[st]
>             elif len(l2[st]) == 0:
>                 if len(l1[st]) > 0:
>                     l2[st] = l1[st]

That's even worse!

Anyway, ignoring all that, if what you are trying to do is just do some 
action based on a set of fixed comparisons that are known at the top of 
the function (and do not mutate depending on the path through the 
function), then you can just cache the comparisons and then compare the 
resulting set of boolean results (note that my examples are NOT based on 
your use-case, it's just pseudo-code which happens to use your expressions):

state = (len(l1[st]) == 0, len(l2[st]) > 0)
if state == (True, False):
elif state == (False, True):

... etc.

If the len() comparisons are tri-state (i.e., in some cases you want to 
know if the length is <, ==, or > 0, depending on one of the other 
comparisons) then you can do something like:

def clamp(foo):
   return min(max(-1, foo), 1)

state = (clamp(cmp(len(l1[st]), 0), cmp(len(l2[st]), 0))
if state == (0, -1):
elif state == (1, -1):

... etc.

I'm not sure this makes it much more readable though - but if you make 
the RHS of those comparisons a symbolic name you might be getting 
somewhere -

ACTION1 = (0, -1)
ACTION2 = (1, -1)
if state == ACTION1:
elif state == ACTION2:

Hope that helps, E.

More information about the Python-list mailing list