[Python-3000] Please re-add __cmp__ to python 3000

Steven Bethard steven.bethard at gmail.com
Wed Oct 31 01:17:17 CET 2007


On 10/30/07, Adam Olsen <rhamph at gmail.com> wrote:
> It's clear to me that the opposition to removing __cmp__ comes down to
> "make the common things easy and the rare things possible".  Removing
> __cmp__ means one of the common things (total ordering) becomes hard.

I don't really think that's it.  I don't see much of a difference in
difficulty between writing::

    class C(TotalOrderingMixin):
        def __lt__(self, other):
            self.foo < other.foo
        def __eq__(self, other):
            self.foo == other.foo

or writing [1] ::

    class C(object):
        def __cmp__(self, other):
            if self.foo < other.foo:
                return -1
            elif self.foo < other.foo:
                return 1
            else:
                return 0

The main motivation seems really to be efficiency for a particular
task.  For some tasks, e.g. sorting, you really only need __lt__, so
going through __cmp__ will just be slower. For other tasks, e.g.
comparing objects with several components, you know you have to do
both the __lt__ and __eq__ comparisons, so it would be wasteful to
make two calls when you know you could do it in one through __cmp__.

So it's not really about making things easier or harder, it's about
making the most efficient tool for the task available.

Steve

[1] Yes, of course, you could just write cmp(self.foo, other.foo), but
this is how it's been written in the rest of the thread, so I have to
assume that it's more representative of real code.
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy


More information about the Python-3000 mailing list