Cleaning up conditionals

Antoon Pardon antoon.pardon at rece.vub.ac.be
Mon Jan 2 09:09:21 EST 2017


Op 31-12-16 om 01:26 schreef Deborah Swanson:
>> On 31 December 2016 at 10:00, Deborah Swanson 
>> <python at deborahswanson.net> wrote:
>>> Oops, indentation was messed up when I copied it into the email.
>> The indentation of your latest message looks completely 
>> broken  now, you can see it here: 
>> https://mail.python.org/pipermail/python-list/2016-December/71
> 7758.html
>
> Hm, I don't know what pipermail does with whitespace but the formatting
> in the message at
> https://mail.python.org/pipermail/python-list/2016-December/717758.html
> is not the same as the message I sent from Outlook.
>
> Again, it should have been:
>
> 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]

I just would like to point out that you could just eliminate each sub-if
and just write this as:

if len(l1[st]) == 0:
    l1[st] = l2[st]
elif len(l2[st]) == 0:
     l2[st] = l1[st]

If we look at the first branch the eliminated test would prevent the
assignment in case l2[st] is empty. But we already know l1[st] is empty.
So we are just eliminating the replacement of an empty field with an
empty field. There is no harm in that and trying to eliminate it just
makes your test unnecessarily complex.

-- 
Antoon Pardon 





More information about the Python-list mailing list