checking if two things do not equal None

Dave Angel davea at davea.name
Sat Mar 29 18:01:03 EDT 2014


 Roy Smith <roy at panix.com> Wrote in message:
> In article <lh7cb4$ntu$2 at news.albasani.net>,
>  Johannes Bauer <dfnsonfsduifb at gmx.de> wrote:
> 
>> On 29.03.2014 20:05, Steven D'Aprano wrote:
>> > On Sat, 29 Mar 2014 11:56:50 -0700, contact.trigon wrote:
>> > 
>> >> if (a, b) != (None, None):
>> >> or
>> >> if a != None != b:
>> >>
>> >> Preference? Pros? Cons? Alternatives?
>> >
>> > if not (a is b is None): ...
>> > 
>> > Or if you prefer:
>> > 
>> > if a is not b is not None: ...
>> 
>> Is this an obfuscated coding contest? Why do you opt for a solution that
>> one has to at least think 2 seconds about when the simplest solution:
>> 
>> if (a is not None) or (b is not None):
>> 
>> is immediately understandable by everyone?
> 
> I agree with that.  But
> 
>> if (a, b) != (None, None):
> 
> seems pretty straight-forward to me too.  In fact, if anything, it seems 
> easier to understand than
> 
>> if (a is not None) or (b is not None):
> 
> I certainly agree that things like
> 
>> if a is not b is not None: ...
> 
> belong in an obfuscated coding contest.  Code gets read a lot more often 
> than it get written.  Make it dead-ass simple to understand, and future 
> generations of programmers who inherit your code will thank you for it.
> 

The other advantage to keeping it simple is it's more than likely
 to be right.  If we take the original form as the spec, we'll
 find that two of the alternatives are not even equivalent.
 

def trigon1(a, b):
    return (a,b) != (None, None)  #master

def trigon2(a, b):
    return a != None != b.  #    different

def steven1(a, b):
    return not(a is b is None)

def steven2(a, b):
    return a is not b is not None  #different

def johannes(a, b):
    return (a is not None) or (b is not None)

table = [
trigon1,
trigon2,
steven1,
steven2,
johannes
]

for func in table:
    print func.__name__
    print func(None, None), func(None, 42), func(42, None),
 func(42, 42), func(42, "never")




-- 
DaveA




More information about the Python-list mailing list