None in comparison

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • r.grimm@science-computing.de

    None in comparison

    Hello,
    I'm a little confused about None in comparison.
    >>id ( None )
    3086100672L
    >>id ( 1 )
    134541104
    >>None < 1
    True
    >>>
    I thought, the id of the object is the last comparison criterion.
    Therefore, None must be greater then 1.
    Where is the behaviour of the comparison defined?. In the __cmp__ of
    int. Or in the global cmp Function?

    Thanks in advance
    Rainer
  • Marc 'BlackJack' Rintsch

    #2
    Re: None in comparison

    On Thu, 17 Jul 2008 23:52:13 -0700, r.grimm wrote:
    Hello,
    I'm a little confused about None in comparison.
    >
    >>>id ( None )
    3086100672L
    >>>id ( 1 )
    134541104
    >>>None < 1
    True
    >>>>
    >
    I thought, the id of the object is the last comparison criterion.
    Obviously that expectation is false. The result of a comparison between
    different types, with no `__cmp__()` method that says otherwise, is a
    arbitrarily but consistent ordering by type. The language doesn't even
    guarantee that it must be consistent in different runs of the same program
    in the same interpreter, just within one run.

    So if you plan to rely on such implementation details, your program is
    broken.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Terry Reedy

      #3
      Re: None in comparison



      Marc 'BlackJack' Rintsch wrote:
      On Thu, 17 Jul 2008 23:52:13 -0700, r.grimm wrote:
      >
      >Hello,
      >I'm a little confused about None in comparison.
      >>
      >>>>id ( None )
      >3086100672L
      >>>>id ( 1 )
      >134541104
      >>>>None < 1
      >True
      >I thought, the id of the object is the last comparison criterion.
      >
      Obviously that expectation is false. The result of a comparison between
      different types, with no `__cmp__()` method that says otherwise, is a
      arbitrarily but consistent ordering by type. The language doesn't even
      guarantee that it must be consistent in different runs of the same program
      in the same interpreter, just within one run.
      >
      So if you plan to rely on such implementation details, your program is
      broken.
      And in 3.0 such arbitrary comparisons are gone.
      >>None < 1
      Traceback (most recent call last):
      File "<pyshell#8 >", line 1, in <module>
      None < 1
      TypeError: unorderable types: NoneType() < int()

      Comment

      Working...