Guido rethinking removal of cmp from sort method

Stefan Behnel stefan_ml at behnel.de
Wed Mar 23 12:51:07 EDT 2011


Antoon Pardon, 23.03.2011 16:14:
> On Wed, Mar 23, 2011 at 02:59:09PM +0100, 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.
>
> Which isn't helpfull if where you decide how they have to be sorted is
> not the place where they are actually sorted.
>
> I have a class that is a priority queue. Elements are added at random but
> are removed highest priority first. The priority queue can have a key or
> a cmp function for deciding which item is the highest priority. It
> can also take a list as an initializor, which will then be sorted.
>
> So this list is sorted within the class but how it is sorted is decided
> outside the class. So I can't do the sort in multiple steps.

That sounds more like a use case for heap sort than for Python's builtin 
list sort. See the heapq module.

Stefan




More information about the Python-list mailing list