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

Steven Bethard steven.bethard at gmail.com
Wed Oct 17 19:27:38 CEST 2007


On 10/17/07, David A. Wheeler <dwheeler at dwheeler.com> wrote:
> I said:
> > I agree with Collin Winter: losing __cmp__ is a loss  (see http://oakwinter.com/code/).
>
> Steven Bethard said:
> >Why can't this just be supplied with a mixin?  Here's a recipe
> >providing the appropriate mixins if you want to define a __key__
> >function:
> >    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/510403
>
> That _works_ from a functional perspective, and if Python3 fails to
> include direct support for __cmp__, then I think providing a built-in
> mixin is necessary.
>
> But mixins for comparison are a BIG LOSER for sort performance
> if your fundamental operator is a cmp-like function.
[snip]
> I did a test (see below), and the mixin using a simulated cmp took
> 50% MORE time to sort a list using Python 2.5

Patient: When I move my arm, it hurts.
Doctor: Well don't move your arm then.

;-)

I'm having troubles coming up with things where the *basic* operator
is really a cmp-like function.  Even in your example, the cmp function
was defined in terms of "less than". If the basic operator is really
"less than", then why define a cmp() function at all? Particularly
since, even in Python 2.5, sorting is faster when you define __lt__
instead of __cmp__::

    class NumberWithLessThan(object):
        def __init__(self, data):
            self.data = data
        def __lt__(self, other):
            return self.data < other.data

    class NumberWithCmp(object):
        def __init__(self, data):
            self.data = data
       def __cmp__(self, other):
           return cmp(self.data, other.data)

    $ python -m timeit -s "import script, random" "data =
[script.NumberWithLessThan(i) for i in xrange(1000)];
random.shuffle(data); data.sort()"
    100 loops, best of 3: 7.93 msec per loop

    $ python -m timeit -s "import script, random" "data =
[script.NumberWithCmp(i) for i in xrange(1000)]; random.shuffle(data);
data.sort()"
    100 loops, best of 3: 10.5 msec per loop

STeVe
-- 
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