Guido rethinking removal of cmp from sort method

Antoon Pardon Antoon.Pardon at rece.vub.ac.be
Fri Mar 25 08:56:23 EDT 2011


On Fri, Mar 25, 2011 at 11:05:17AM +0100, Stefan Behnel wrote:
> Antoon Pardon, 25.03.2011 10:21:
> >On Thu, Mar 24, 2011 at 11:49:53PM +0000, Steven D'Aprano wrote:
> >>On Thu, 24 Mar 2011 17:47:05 +0100, Antoon Pardon wrote:
> >>
> >>>However since that seems to be a problem for you I will be more
> >>>detailed. The original poster didn't ask for cases in which cmp was
> >>>necessary, he asked for cases in which not using cmp was cumbersome.
> >>
> >>I'm the original poster, and that's not what I said. I said:
> >>
> >>"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."
> >>
> >>You'll notice that I said nothing about whether writing the code was easy
> >>or cumbersome, and nothing about readability.
> >
> >Well fine. I should have realised the question was just a pretense and that
> >there really never was any intention to consider the reactions, because
> >the answer is already fixed.
> 
> I don't think it is. It's just not as simple as "see, I have an
> artificial use case here, so you *must* put the feature back in the
> language".

Look we are provided with the cmp_to_key function. If something doesn't work
with that function or performs realy bad with that function, it means either
the function has a bug in it or something the function relies on, has a bug
in it. In either case you just fix the bug.

My guess is that cmp_to_key is implemented similarly as my example below.
So we simply have a constant overhead before we call the user provided
cmp function on the items to be sorted. So if this would fail to sort the
items or started to perform really badly (more than can be expected
by the overhead illustrated below) it would indicate an internal python
problem, either in the implementation of cmp_to_key or elsewhere.

Well if python has an internal problem, my expectation is that they will
fix the problem, instead of providing a cmp argument to sort as a work around.
So the outcome was fixed from the beginning.


def cmp_to_key(func):

  class cmpkey:
    def __init__(self, arg):
      self.data = arg
      self.cmp = func

  def __lt__(self, other):
    return self.cmp(self.data, other.data) < 0

  def __le__(self, other):
    return self.cmp(self.data, other.data) <= 0

  ...

  return cmpkey



More information about the Python-list mailing list