Cleaning up conditionals

Deborah Swanson python at deborahswanson.net
Sat Dec 31 23:17:15 EST 2016


Dennis Lee Bieber
> Sent: Saturday, December 31, 2016 7:29 PM
> 
> On Sat, 31 Dec 2016 14:35:46 -0800, "Deborah Swanson" 
> <python at deborahswanson.net> declaimed the following:
> 
> >Here's the function I have so far:
> >
> >def comprows(l1,l2,st,ki,no):
> >    ret = ''
> >    labels = {st: 'st/co', ki: 'kind', no: 'notes'}
> >    for v in (st,ki,no):
> >        if len(l1[v]) == 0 and len(l2[v]) != 0:
> >            l1[v] = l2[v]
> >        elif len(l2[v]) == 0 and len(l1[v]) != 0:
> >            l2[v] = l1[v]
> >        elif l1[v] != l2[v]:
> >            ret += ", " + labels[v] + " diff" if len(ret) > 0 else 
> >            labels[v] + " diff"
> >    return ret
> >
> 
> 	Now, out of all that, which is the most likely one to 
> be activated? Granted, it's too early to be considering 
> optimizations, but... If you expect the common situation to 
> be the "l1 != l2", the order of your tests means you do two 
> failing tests before getting to the most likely version.
> -- 
> 	Wulfraed                 Dennis Lee Bieber         AF6VN
>     wlfraed at ix.netcom.com    HTTP://wlfraed.home.netcom.com/

I see what you're saying, but actually it's fairly rare that l1[v] and
l2[v] are different. I just need to catch it and look at it when they
are. It usually means that either the spelling/typing in the listing was
wrong and my code didn't fix it, or in rarer cases (now), my code is
buggy. I probably won't ever be able to dispense with this test since
people are always typoing. They don't always copy from their previous
listings and sometimes it looks like they're trying to retype it from
memory, and messing up.
D.



More information about the Python-list mailing list