Guido rethinking removal of cmp from sort method

Ian Kelly ian.g.kelly at gmail.com
Thu Mar 24 11:06:44 EDT 2011


On Thu, Mar 24, 2011 at 3:23 AM, Antoon Pardon
<Antoon.Pardon at rece.vub.ac.be> wrote:
> Sure I can do that. I can do lots of things like writing a CMP class
> that I will use as a key and where I can implement the logic for
> comparing the original objects, which I otherwise would have put in a
> cmp function. I thought this wasn't about how one can get by without
> the cmp argument. This was about cases where the cmp argument was the
> more beautiful or natural way to handle this case.

That's not what you wrote before.  You wrote "I can't do the sort in
multiple steps."  I was just responding to what you wrote.

> I think I have provided such a case. If you don't agree then
> don't just give examples of what I can do, argue how your solution
> would be a less ugly or more natural way to handle this.

Well, the alternative is to generate the cmp function from the
externally selected keys dynamically at runtime, is it not?  Something
like this:

def get_cmp(keys):
    def cmp(a, b):
        for (key, reversed) in keys:
            if reversed:
                result = cmp(key(b), key(a))
            else:
                result = cmp(key(a), key(b))
            if result != 0:
                return result
        return result
    return cmp

I fail to see how that is any more natural than my solution, unless
you have another way of doing it.  And it's certainly not going to be
any faster.



More information about the Python-list mailing list