Cleaning up conditionals

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Sat Dec 31 01:54:12 EST 2016


"Deborah Swanson" <python at deborahswanson.net> writes:

> Michael Torrie wrote:
>> On 12/30/2016 05:26 PM, Deborah Swanson wrote:
>> > I'm still wondering if these 4 lines can be collapsed to one or two 
>> > lines.
>> 
>> If the logic is clearly expressed in the if blocks that you 
>> have, I don't see why collapsing an if block into one or two 
>> lines would even be desirable.  Making a clever one-liner out 
>> of something isn't always a good thing.  In fact some 
>> programmers don't like to use the ternary operator or 
>> conditional expressions, preferring to use explicit if block logic.
>> 
>
> Maybe it isn't always a good thing, but learning the capabilities of
> python is. Besides, if the concern is future maintenance, a lot would
> depend on the proficiency of those expected to maintain the code.

One line:

    l1[st], l2[st] = (l1[st] or l2[st]), (l2[st] or l1[st])

(The parentheses are redundant.)

Two lines:

    l1[st] = l1[st] or l2[st]
    l2[st] = l2[st] or l1[st]

Both of these store the same value back in the given field if it's
considered true (non-empty), else they store the corresponding value
from the other record in the hope that it would be considered true
(non-empty).



More information about the Python-list mailing list