Guido rethinking removal of cmp from sort method

Antoon Pardon Antoon.Pardon at rece.vub.ac.be
Mon Mar 28 05:06:20 EDT 2011


On Fri, Mar 25, 2011 at 10:40:03PM +0000, Steven D'Aprano wrote:
> On Fri, 25 Mar 2011 13:56:23 +0100, Antoon Pardon wrote:
> 
> > 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.
> 
> No, it means that the trade-offs made by cmp_to_key, or by sorting with a 
> key function, do not interact well with what you are trying to do.

So and if you find something like that, what stops the dev team from
saying that python is just not suited to deal with such a problem?
Specifically as your analogy points in that direction as your dillema has
a strategy that is good for python on the one hand and a problem not
suitable for python on the other hand.

> [analgy of python lists, that double in size when full vs
>  memory management on a 8K device ]

> So the question is, being completely general, as there any *real-world* 
> (not artificial, made-up) uses where sorting with a comparison function 
> works but a key function doesn't? I'm not limiting you to cases where you 
> have a shortage of memory but lots of time available. There may be other 
> design choices where key-based sorting sucks. We already know that some 
> people just prefer the look and feel of writing and using cmp functions. 
> Is there anything else?

Asking for *real-world* uses is just how the python community smothers
requests. Should someone come with a use case, I expect the following
to happenr: One will go over the case with magnifying glasses in
search for any possible way in which it could be rewritten so as not
to need the cmp anyway. This will probably not be a big problem because
the person with the use case will probably have given a simplified example
to explain the overal picture. However most people responding will
ingnore the overal picture and focus on details that may very well work
on the simplified example but may not in the specific real-world case.
But since going over all details to argue the case will be too time
consuming, in the end the person with the use case will simply fail to
convince.

Forcing people to use a key-function, will produce cases where python
will ask for more memory and take longer to sort, than allowing to provide
a cmp function. This for the simple reason that for some order-functions,
the only way to write a key-function will be to write a specific class,
that will implement the order with the same kind of code that otherwise
would have been put in the cmp function, making the trade off: memory use
vs speed no longer a trade off.

If the existance alone of such cases isn't enough to reverse the decision
about the cmp-argument of the sort-method. My guess is that nothing will.

-- 
Antoon Pardon



More information about the Python-list mailing list