Guido rethinking removal of cmp from sort method

Carl Banks pavlovevidence at gmail.com
Wed Mar 23 14:47:26 EDT 2011


On Mar 23, 10:51 am, Stefan Behnel <stefan... at behnel.de> wrote:
> Carl Banks, 23.03.2011 18:23:
>
>
>
>
>
> > On Mar 23, 6:59 am, Stefan Behnel wrote:
> >> Antoon Pardon, 23.03.2011 14:53:
>
> >>> On Sun, Mar 13, 2011 at 12:59:55PM +0000, Steven D'Aprano wrote:
> >>>> The removal of cmp from the sort method of lists is probably the most
> >>>> disliked change in Python 3. On the python-dev mailing list at the
> >>>> moment, Guido is considering whether or not it was a mistake.
>
> >>>> If anyone has any use-cases for sorting with a comparison function that
> >>>> either can't be written using a key function, or that perform really
> >>>> badly when done so, this would be a good time to speak up.
>
> >>> How about a list of tuples where you want them sorted first item in ascending
> >>> order en second item in descending order.
>
> >> You can use a stable sort in two steps for that.
>
> > How about this one: you have are given an obscure string collating
> > function implented in a C library you don't have the source to.
>
> > Or how about this: I'm sitting at an interactive session and I have a
> > convenient cmp function but no convenient key, and I care more about
> > the four minutes it'd take to whip up a clever key function or an
> > adapter class than the 0.2 seconds I'd save to on sorting time.
>
> As usual with Python, it's just an import away:
>
> http://docs.python.org/library/functools.html#functools.cmp_to_key
>
> I think this is a rare enough use case to merit an import rather than being
> a language feature.

The original question posted here was, "Is there a use case for cmp?"
There is, and your excuse-making doesn't change the fact.  It's the
most natural way to sort sometimes; that's a use case.  We already
knew it could be worked around.

It's kind of ridiculous to claim that cmp adds much complexity (it's
maybe ten lines of extra C code), so the only reason not to include it
is that it's much slower than using key.  Not including it for that
reason would be akin to the special-casing of sum to prevent strings
from being concatenated, although omitting cmp would not be as drastic
since it's not a special case.

Do we omit something that's useful but potentially slow?  I say no.


Carl Banks



More information about the Python-list mailing list