Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #69347

Re: checking if two things do not equal None

From Dave Angel <davea@davea.name>
Subject Re: checking if two things do not equal None
Date 2014-03-29 18:01 -0400
Organization news.gmane.org
References <0245aca0-c6b7-493a-aa52-2c3ef6462dbd@googlegroups.com> <5337195f$0$29994$c3e8da3$5496439d@news.astraweb.com> <lh7cb4$ntu$2@news.albasani.net> <roy-B5E8CC.17072029032014@news.panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.8699.1396130164.18130.python-list@python.org> (permalink)

Show all headers | View raw


 Roy Smith <roy@panix.com> Wrote in message:
> In article <lh7cb4$ntu$2@news.albasani.net>,
>  Johannes Bauer <dfnsonfsduifb@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

checking if two things do not equal None contact.trigon@gmail.com - 2014-03-29 11:56 -0700
  Re: checking if two things do not equal None Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-29 19:05 +0000
    Re: checking if two things do not equal None contact.trigon@gmail.com - 2014-03-29 12:23 -0700
    Re: checking if two things do not equal None Lele Gaifax <lele@metapensiero.it> - 2014-03-29 20:24 +0100
    Re: checking if two things do not equal None Johannes Bauer <dfnsonfsduifb@gmx.de> - 2014-03-29 22:01 +0100
      Re: checking if two things do not equal None Roy Smith <roy@panix.com> - 2014-03-29 17:07 -0400
        Re: checking if two things do not equal None Johannes Bauer <dfnsonfsduifb@gmx.de> - 2014-03-29 22:55 +0100
          Re: checking if two things do not equal None Johannes Bauer <dfnsonfsduifb@gmx.de> - 2014-03-29 23:02 +0100
        Re: checking if two things do not equal None Dave Angel <davea@davea.name> - 2014-03-29 18:01 -0400
          Re: checking if two things do not equal None contact.trigon@gmail.com - 2014-03-29 16:20 -0700
        Re: checking if two things do not equal None Tim Chase <python.list@tim.thechases.com> - 2014-03-29 17:36 -0500
          Re: checking if two things do not equal None Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-30 02:24 +0000
            Re: checking if two things do not equal None Roy Smith <roy@panix.com> - 2014-03-29 22:43 -0400
              Re: checking if two things do not equal None Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-03-29 22:04 -0500
        Re: checking if two things do not equal None Roy Smith <roy@panix.com> - 2014-03-29 18:41 -0400
        Re: checking if two things do not equal None Tim Chase <tim@thechases.com> - 2014-03-29 17:46 -0500
          Re: checking if two things do not equal None Roy Smith <roy@panix.com> - 2014-03-29 18:51 -0400
        Re: checking if two things do not equal None Chris Angelico <rosuav@gmail.com> - 2014-03-30 10:17 +1100
        Re: checking if two things do not equal None Tim Chase <python.list@tim.thechases.com> - 2014-03-29 20:19 -0500
        Re: checking if two things do not equal None Chris Angelico <rosuav@gmail.com> - 2014-03-30 12:37 +1100
        Re: checking if two things do not equal None Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-30 02:04 +0000
          Re: checking if two things do not equal None Chris Angelico <rosuav@gmail.com> - 2014-03-30 13:15 +1100
            Re: checking if two things do not equal None Roy Smith <roy@panix.com> - 2014-03-29 22:39 -0400
              Re: checking if two things do not equal None Rustom Mody <rustompmody@gmail.com> - 2014-03-29 19:54 -0700
                Re: checking if two things do not equal None Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-30 06:08 +0000
                Re: checking if two things do not equal None Roy Smith <roy@panix.com> - 2014-03-30 08:21 -0400
                Re: checking if two things do not equal None MRAB <python@mrabarnett.plus.com> - 2014-03-30 14:58 +0100
                Re: checking if two things do not equal None Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-03-31 11:48 +1300
              Re: checking if two things do not equal None Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-03-30 19:41 +1300
                Re: checking if two things do not equal None Marko Rauhamaa <marko@pacujo.net> - 2014-03-30 10:37 +0300
            Re: checking if two things do not equal None Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-30 05:54 +0000
              Re: checking if two things do not equal None Chris Angelico <rosuav@gmail.com> - 2014-03-30 17:17 +1100
              Re: checking if two things do not equal None Ben Finney <ben+python@benfinney.id.au> - 2014-03-30 17:52 +1100
              Re: checking if two things do not equal None Chris Angelico <rosuav@gmail.com> - 2014-03-30 18:36 +1100
      Re: checking if two things do not equal None Ethan Furman <ethan@stoneleaf.us> - 2014-03-29 16:20 -0700
  Re: checking if two things do not equal None Terry Reedy <tjreedy@udel.edu> - 2014-03-29 19:02 -0400
  Re: checking if two things do not equal None Jeremy Sanders <jeremy@jeremysanders.net> - 2014-03-31 09:56 +0200
    Re: checking if two things do not equal None Abe <contact.trigon@gmail.com> - 2014-03-31 10:28 -0700
      Re: checking if two things do not equal None Moritz Emanuel Beber <moritz.beber@gmail.com> - 2014-03-31 21:22 +0200

csiph-web