Python 3 __cmp__ semantic change?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johannes Bauer

    Python 3 __cmp__ semantic change?

    Hello group,

    I'm porting some code of mine to Python 3. One class has the __cmp__
    operator overloaded, but comparison doesn't seem to work anymore with that:

    Traceback (most recent call last):
    File "./parse", line 25, in <module>
    print(x < y)
    TypeError: unorderable types: IP() < IP()

    Was there some kind of semantic change?

    Kind regards,
    Johannes

    --
    "Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
    verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
    -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
    <48d8bf1d$0$751 0$5402220f@news .sunrise.ch>
  • Inyeol.Lee@gmail.com

    #2
    Re: Python 3 __cmp__ semantic change?

    On Nov 20, 1:18 pm, Johannes Bauer <dfnsonfsdu...@ gmx.dewrote:
    Hello group,
    >
    I'm porting some code of mine to Python 3. One class has the __cmp__
    operator overloaded, but comparison doesn't seem to work anymore with that:
    >
    Traceback (most recent call last):
      File "./parse", line 25, in <module>
        print(x < y)
    TypeError: unorderable types: IP() < IP()
    >
    Was there some kind of semantic change?
    Overload __lt__ method.

    Inyeol

    Comment

    • Johannes Bauer

      #3
      Re: Python 3 __cmp__ semantic change?

      Inyeol.Lee@gmai l.com schrieb:
      On Nov 20, 1:18 pm, Johannes Bauer <dfnsonfsdu...@ gmx.dewrote:
      >Hello group,
      >>
      >I'm porting some code of mine to Python 3. One class has the __cmp__
      >operator overloaded, but comparison doesn't seem to work anymore with that:
      >>
      >Traceback (most recent call last):
      > File "./parse", line 25, in <module>
      > print(x < y)
      >TypeError: unorderable types: IP() < IP()
      >>
      >Was there some kind of semantic change?
      >
      Overload __lt__ method.
      Well, of course I could do that, but the python doc says:

      "Called by comparison operations if rich comparison (see above) is not
      defined."


      And my code works just fine with 2.5 - only on 3.0 it doesn't work
      anymore. Why is that?

      Regards,
      Johannes

      --
      "Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
      verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
      -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
      <48d8bf1d$0$751 0$5402220f@news .sunrise.ch>

      Comment

      • Christian Heimes

        #4
        Re: Python 3 __cmp__ semantic change?

        Johannes Bauer wrote:
        Hello group,
        >
        I'm porting some code of mine to Python 3. One class has the __cmp__
        operator overloaded, but comparison doesn't seem to work anymore with that:
        __cmp__ is gone

        Christian

        Comment

        • Steve Holden

          #5
          Re: Python 3 __cmp__ semantic change?

          Johannes Bauer wrote:
          Inyeol.Lee@gmai l.com schrieb:
          >On Nov 20, 1:18 pm, Johannes Bauer <dfnsonfsdu...@ gmx.dewrote:
          >>Hello group,
          >>>
          >>I'm porting some code of mine to Python 3. One class has the __cmp__
          >>operator overloaded, but comparison doesn't seem to work anymore with that:
          >>>
          >>Traceback (most recent call last):
          >> File "./parse", line 25, in <module>
          >> print(x < y)
          >>TypeError: unorderable types: IP() < IP()
          >>>
          >>Was there some kind of semantic change?
          >Overload __lt__ method.
          >
          Well, of course I could do that, but the python doc says:
          >
          "Called by comparison operations if rich comparison (see above) is not
          defined."

          >
          And my code works just fine with 2.5 - only on 3.0 it doesn't work
          anymore. Why is that?
          Well the Python 2.5 documentation can't be regarded as a reliable guide
          to what to expect in 3.0 ...

          You will observe that __cmp__ no longer appears in the index:



          regards
          Steve
          --
          Steve Holden +1 571 484 6266 +1 800 494 3119
          Holden Web LLC http://www.holdenweb.com/

          Comment

          • Ben Finney

            #6
            Re: Python 3 __cmp__ semantic change?

            Steve Holden <steve@holdenwe b.comwrites:
            You will observe that __cmp__ no longer appears in the index:
            >
            http://docs.python.org/dev/3.0/genindex-_.html
            I searched in vain for an official description of this changed
            behaviour. Where can we find an official description of how
            comparisons are different in Python 3.0?

            --
            \ “[Entrenched media corporations will] maintain the status quo, |
            `\ or die trying. Either is better than actually WORKING for a |
            _o__) living.” —ringsnake.li vejournal.com, 2007-11-12 |
            Ben Finney

            Comment

            • Steve Holden

              #7
              Re: Python 3 __cmp__ semantic change?

              Ben Finney wrote:
              Steve Holden <steve@holdenwe b.comwrites:
              >
              >You will observe that __cmp__ no longer appears in the index:
              >>
              >http://docs.python.org/dev/3.0/genindex-_.html
              >
              I searched in vain for an official description of this changed
              behaviour. Where can we find an official description of how
              comparisons are different in Python 3.0?
              >
              If it's not present then it would be worth reporting it as a 3.0 bug -
              there's still time to get it in, as the release isn't due until early
              December.

              regards
              Steve
              --
              Steve Holden +1 571 484 6266 +1 800 494 3119
              Holden Web LLC http://www.holdenweb.com/

              Comment

              • Terry Reedy

                #8
                Re: Python 3 __cmp__ semantic change?

                Ben Finney wrote:
                Steve Holden <steve@holdenwe b.comwrites:
                >
                >You will observe that __cmp__ no longer appears in the index:
                >>
                >http://docs.python.org/dev/3.0/genindex-_.html
                >
                I searched in vain for an official description of this changed
                behaviour. Where can we find an official description of how
                comparisons are different in Python 3.0?
                I was going to say "look in "What's New", but the __cmp__ removal is
                missing. So I filed


                Comment

                • Christian Heimes

                  #9
                  Re: Python 3 __cmp__ semantic change?

                  Terry Reedy wrote:
                  I was going to say "look in "What's New", but the __cmp__ removal is
                  missing. So I filed
                  http://bugs.python.org/issue4372
                  The whatsnew section of Python 3.0 is still empty. Guido didn't had time
                  to write it. http://bugs.python.org/issue2306

                  Comment

                  • Johannes Bauer

                    #10
                    Re: Python 3 __cmp__ semantic change?

                    Steve Holden schrieb:
                    If it's not present then it would be worth reporting it as a 3.0 bug -
                    there's still time to get it in, as the release isn't due until early
                    December.
                    Seems it was removed on purpose - I'm sure there was a good reason for
                    that, but may I ask why? Instead of the sleek __cmp__ function I had
                    earlier, I now have code like:


                    def __lt__(self, other):
                    return self.__cmp__(ot her) < 0

                    def __le__(self, other):
                    return self.__cmp__(ot her) < 0

                    def __gt__(self, other):
                    return self.__cmp__(ot her) 0

                    def __ge__(self, other):
                    return self.__cmp__(ot her) >= 0

                    Does anyone know the reason why __cmp__ was discarded?

                    Kind regards,
                    Johannes

                    --
                    "Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
                    verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
                    -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
                    <48d8bf1d$0$751 0$5402220f@news .sunrise.ch>

                    Comment

                    • Terry Reedy

                      #11
                      Re: Python 3 __cmp__ semantic change?

                      Christian Heimes wrote:
                      Terry Reedy wrote:
                      >I was going to say "look in "What's New", but the __cmp__ removal is
                      >missing. So I filed
                      >http://bugs.python.org/issue4372
                      >
                      The whatsnew section of Python 3.0 is still empty. Guido didn't had time
                      to write it. http://bugs.python.org/issue2306
                      What's New in Python 3.0 is incomplete but definitely not empty, and it
                      is part of the doc set.

                      Comment

                      • skip@pobox.com

                        #12
                        Re: Python 3 __cmp__ semantic change?


                        JohannesSeems it was removed on purpose - I'm sure there was a good
                        Johannesreason for that, but may I ask why?

                        Start here:



                        Also, a comment to this blog post suggests creating a CmpMixin:



                        Skip

                        Comment

                        • Terry Reedy

                          #13
                          Re: Python 3 __cmp__ semantic change?

                          Johannes Bauer wrote:
                          Steve Holden schrieb:
                          >
                          >If it's not present then it would be worth reporting it as a 3.0 bug -
                          >there's still time to get it in, as the release isn't due until early
                          >December.
                          >
                          Seems it was removed on purpose - I'm sure there was a good reason for
                          that, but may I ask why? Instead of the sleek __cmp__ function I had
                          earlier, I now have code like:
                          >
                          >
                          def __lt__(self, other):
                          return self.__cmp__(ot her) < 0
                          >
                          def __le__(self, other):
                          return self.__cmp__(ot her) < 0
                          >
                          def __gt__(self, other):
                          return self.__cmp__(ot her) 0
                          >
                          def __ge__(self, other):
                          return self.__cmp__(ot her) >= 0
                          >
                          Does anyone know the reason why __cmp__ was discarded?
                          See previous threads, including recent one about sorting.

                          Comment

                          • George Sakkis

                            #14
                            Re: Python 3 __cmp__ semantic change?

                            On Nov 20, 6:58 pm, s...@pobox.com wrote:
                                JohannesSeems it was removed on purpose - I'm sure there was a good
                                Johannesreason for that, but may I ask why?
                            >
                            Start here:
                            >
                                http://www.mail-archive.com/python-3.../msg11474.html
                            >
                            Also, a comment to this blog post suggests creating a CmpMixin:
                            >
                               http://oakwinter.com/code/porting-setuptools-to-py3k/
                            >
                            Skip
                            Dropping __cmp__ without providing implicit or at least easy explicit
                            [1] total ordering is (was?) a mistake; it opens the door to subtle
                            bugs or redundant boilerplate code.

                            [1] E.g. http://code.activestate.com/recipes/576529/

                            Comment

                            • Duncan Booth

                              #15
                              Re: Python 3 __cmp__ semantic change?

                              Johannes Bauer <dfnsonfsduifb@ gmx.dewrote:
                              Seems it was removed on purpose - I'm sure there was a good reason for
                              that, but may I ask why? Instead of the sleek __cmp__ function I had
                              earlier, I now have code like:
                              >
                              >
                              def __lt__(self, other):
                              return self.__cmp__(ot her) < 0
                              >
                              def __le__(self, other):
                              return self.__cmp__(ot her) < 0
                              I hope you actually have <= here.
                              >
                              def __gt__(self, other):
                              return self.__cmp__(ot her) 0
                              >
                              def __ge__(self, other):
                              return self.__cmp__(ot her) >= 0
                              >
                              Does anyone know the reason why __cmp__ was discarded?
                              I think it was because __cmp__ was the backward compatible fallback for
                              the newer rich comparison methods and Python 3 cleans up a lot of stuff
                              left in just for backward compatibility. In this case it is a cleanup
                              too far as in most cases (i.e. those cases where you don't need the full
                              complexity of the rich comparisons) __cmp__ is a much simpler solution.

                              See http://mail.python.org/pipermail/pyt...ch/034073.html
                              for Guido's original thoughts. Also, once upon a time pep-3000 referred
                              to the removal of __cmp__ but I can't find it in any of the current
                              peps. See

                              and

                              where the reference to removal of __cmp__ became "Comparison s other than
                              ``==`` and ``!=`` between disparate types will raise an exception unless
                              explicitly supported by the type" and the reference to Guido's email
                              about removing __cmp__ was also removed.


                              --
                              Duncan Booth http://kupuguy.blogspot.com

                              Comment

                              Working...