comparison with None

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan G Isaac

    #1

    comparison with None

    >>None >= 0
    False
    >>None <= 0
    True

    Explanation appreciated.

    Thanks,
    Alan Isaac
  • Terry Reedy

    #2
    Re: comparison with None


    "Alan G Isaac" <aisaac@america n.eduwrote in message
    news:3IqdnfRnCN WPC7vbnZ2dnUVZ_ jqdnZ2d@rcn.net ...
    | >>None >= 0
    | False
    | >>None <= 0
    | True
    |
    | Explanation appreciated.

    Should be in the reference manual section on comparisons.



    Comment

    • Steven Howe

      #3
      Re: comparison with None

      Alan G Isaac wrote:
      >>None >= 0
      False
      >>None <= 0
      True
      >
      Explanation appreciated.
      >
      Thanks,
      Alan Isaac
      >
      I've read and found that 'None' comparisons is not always a good idea.
      Better to:
      from types import NoneType

      x = None
      if type( x ) == NoneType:
      # true
      < code >
      else:
      # false; do something else.
      < more code >

      Steven Howe

      Comment

      • Paul McGuire

        #4
        Re: comparison with None

        On Apr 18, 5:19 pm, Steven Howe <howe.ste...@gm ail.comwrote:
        Alan G Isaac wrote:
        >>None >= 0
        False
        >>None <= 0
        True
        >
        Explanation appreciated.
        >
        Thanks,
        Alan Isaac
        >
        I've read and found that 'None' comparisons is not always a good idea.
        Better to:
        from types import NoneType
        >
        x = None
        if type( x ) == NoneType:
        # true
        < code >
        else:
        # false; do something else.
        < more code >
        >
        Steven Howe
        None is a singleton - there is but one None and no other. The only
        comparisons that make sense with None are "is" or "is not". type(x)
        == NoneType is unnecessary, x is None is sufficient.
        >>x = None
        >>x is None
        True
        >>y = None
        >>x is y
        True
        >>z = object()
        >>z is None
        False
        >>z is not None
        True
        >>x is not None
        False
        >>y is not None
        False

        -- Paul


        Comment

        • brzrkr0@gmail.com

          #5
          Re: comparison with None

          On Apr 18, 3:19 pm, Steven Howe <howe.ste...@gm ail.comwrote:
          I've read and found that 'None' comparisons is not always a good idea.
          Better to:
          from types import NoneType
          >
          x = None
          if type( x ) == NoneType:
          # true
          < code >
          else:
          # false; do something else.
          < more code >
          >
          Steven Howe
          Is that any better than this?

          if x is None:
          # do something
          else:
          # do something else

          Comment

          • Michael Hoffman

            #6
            Re: comparison with None

            brzrkr0@gmail.c om wrote:
            On Apr 18, 3:19 pm, Steven Howe <howe.ste...@gm ail.comwrote:
            >I've read and found that 'None' comparisons is not always a good idea.
            >Better to:
            >from types import NoneType
            >>
            >x = None
            >if type( x ) == NoneType:
            > # true
            > < code >
            >else:
            > # false; do something else.
            > < more code >
            >>
            >Steven Howe
            >
            Is that any better than this?
            >
            if x is None:
            # do something
            else:
            # do something else
            To the contrary, it's not as good. "if x is None" is much clearer, and
            probably faster.
            --
            Michael Hoffman

            Comment

            • Gary Herron

              #7
              Re: comparison with None

              Alan G Isaac wrote:
              >>None >= 0
              False
              >>None <= 0
              True
              >
              Explanation appreciated.
              >
              Thanks,
              Alan Isaac
              >
              So that we can sort lists of objects, even when the objects of are
              different types, Python guarantees to supply a unique and consistent
              ordering of any two objects. The definition of Python does not specify
              what that ordering is -- that's implementation dependent -- but any two
              objects of any two types *do* have an ordering and that ordering will
              always be the same.

              So in your implementation None is less than 0 (and probably less than
              any integer). Given that, your two observations above are consistent.

              Gary Herron

              Comment

              • Gary Herron

                #8
                Re: comparison with None

                brzrkr0@gmail.c om wrote:
                On Apr 18, 3:19 pm, Steven Howe <howe.ste...@gm ail.comwrote:
                >
                >I've read and found that 'None' comparisons is not always a good idea.
                >Better to:
                >from types import NoneType
                >>
                >x = None
                >if type( x ) == NoneType:
                > # true
                > < code >
                >else:
                > # false; do something else.
                > < more code >
                >>
                >Steven Howe
                >>
                >
                Is that any better than this?
                >
                if x is None:
                # do something
                else:
                # do something else
                >
                No. using
                if x is None:
                is the recommended way.

                Gary Herron

                Comment

                • Robert Kern

                  #9
                  Re: comparison with None

                  Steven Howe wrote:
                  Alan G Isaac wrote:
                  > >>None >= 0
                  >False
                  > >>None <= 0
                  >True
                  >>
                  >Explanation appreciated.
                  >>
                  >Thanks,
                  >Alan Isaac
                  >>
                  I've read and found that 'None' comparisons is not always a good idea.
                  Better to:
                  from types import NoneType
                  >
                  x = None
                  if type( x ) == NoneType:
                  # true
                  < code >
                  else:
                  # false; do something else.
                  < more code >
                  The recommended idiom is to test for "x is None".

                  --
                  Robert Kern

                  "I have come to believe that the whole world is an enigma, a harmless enigma
                  that is made terrible by our own mad attempt to interpret it as though it had
                  an underlying truth."
                  -- Umberto Eco

                  Comment

                  • Alan Isaac

                    #10
                    Re: comparison with None

                    "Terry Reedy" <tjreedy@udel.e duwrote in message
                    news:mailman.66 86.1176934558.3 2031.python-list@python.org ...
                    Should be in the reference manual section on comparisons.
                    Only to this extent:
                    The official home of the Python Programming Language


                    objects of different types always compare unequal, and are ordered
                    consistently but arbitrarily.

                    (This unusual definition of comparison was used to simplify the
                    definition of operations like sorting and the in and not in
                    operators.
                    In the future, the comparison rules for objects of different types
                    are
                    likely to change.)

                    ... Most other types compare unequal unless they are the same
                    object;
                    the choice whether one object is considered smaller or larger than
                    another one is made arbitrarily but consistently within one
                    execution
                    of a program.

                    This does not provide a direct answer to "why" None comparisons.
                    (As far as I can tell, None is less than any object.)

                    However, Gary Herron's explanation makes sense: this provides a stable
                    sort when None is involved, and meets the criterion that objects of
                    different types must always compare unequal. However this would also
                    be true if None always compared greater than any object, and the current
                    behavior does not seem to be guaranteed.

                    Is that about right?

                    Cheers,
                    Alan Isaac


                    Comment

                    • Steven D'Aprano

                      #11
                      Re: comparison with None

                      On Wed, 18 Apr 2007 15:19:26 -0700, Steven Howe wrote:
                      I've read and found that 'None' comparisons is not always a good idea.
                      You're probably thinking of testing against None with equality:

                      if x == None: do_something()

                      That can go wrong if x is a class that has an overly-broad concept of
                      equality, e.g.:

                      class FalseKlass:
                      def __eq__(self, other):
                      # equal to anything that is False
                      return not other

                      Better to:
                      from types import NoneType
                      >
                      x = None
                      if type( x ) == NoneType:
                      # true
                      < code >
                      else:
                      # false; do something else.
                      < more code >

                      Not necessary. Since None is a guaranteed singleton, the only test you
                      need to make is "if x is None: ...".

                      But if you wanted to do extra work unnecessarily, a less unnecessary
                      amount of extra work would be:

                      if type(x) == type(None): ...

                      You don't need to look up the type of None in the types module when you
                      can easily get it from the type() function.



                      --
                      Steven.

                      Comment

                      • Bruno Desthuilliers

                        #12
                        Re: comparison with None

                        Steven Howe a écrit :
                        (snip)
                        I've read and found that 'None' comparisons is not always a good idea.
                        Better to:
                        from types import NoneType
                        >
                        x = None
                        if type( x ) == NoneType:
                        # true
                        < code >
                        else:
                        # false; do something else.
                        < more code >
                        Actually, None is garanteed to be a singleton, so the idiomatic way is
                        to use an identity test:

                        if x is None:
                        # code here

                        But this doesn't answer the OP...

                        HTH

                        Comment

                        • Alex Martelli

                          #13
                          Re: comparison with None

                          Alan Isaac <aisaac@america n.eduwrote:
                          currently documented behavior:
                          "objects of different types always compare unequal".
                          Where is that documented? URL please?
                          >>1.0 == 1
                          True
                          >>type(1.0), type(1)
                          (<type 'float'>, <type 'int'>)

                          here, just as an example, are two objects of different types that
                          compare equal; therefore that "documented behavior" is flat wrong and
                          needs to be fixed.


                          Alex

                          Comment

                          • Alex Martelli

                            #14
                            Re: comparison with None

                            Steven D'Aprano <steve@REMOVE.T HIS.cybersource .com.auwrote:
                            But if you wanted to do extra work unnecessarily, a less unnecessary
                            amount of extra work would be:
                            >
                            if type(x) == type(None): ...
                            Of course, like the original poster's proposal, this CAN be faked out
                            (while the 'is' test cannot, one more weird reason why it's better):
                            >>class metaWeird(type) :
                            .... def __eq__(self, other): return True
                            ....
                            >>class Weird: __metaclass__ = metaWeird
                            ....
                            >>x = Weird()
                            >>type(x) == type(None)
                            True


                            (warning to all innocent bystanders: don't try this at home, kids!-)


                            Alex

                            Comment

                            • Tommy Grav

                              #15
                              Re: comparison with None


                              On Apr 19, 2007, at 11:00 PM, Alex Martelli wrote:
                              Alan Isaac <aisaac@america n.eduwrote:
                              >
                              >currently documented behavior:
                              >"objects of different types always compare unequal".
                              >
                              Where is that documented? URL please?
                              >
                              >>>1.0 == 1
                              True
                              >>>type(1.0), type(1)
                              (<type 'float'>, <type 'int'>)
                              >
                              Isn't this an example of numerical comparison (= or !=) versus
                              object comparison (is or is not). I think the documentation needs
                              to state that there is a difference between the two types.

                              Cheers
                              Tommy

                              Comment

                              Working...