This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients LeWiemann, gvanrossum, rhettinger, tixxit
Date 2009-12-05.07:04:45
SpamBayes Score 4.0356607e-14
Marked as misclassified No
Message-id <1259996689.47.0.040805574901.issue1771@psf.upfronthosting.co.za>
In-reply-to
Content
FWIW, we had a long discussion on comp.lang.python and the net result
was that no use cases were found that required a cmp function.  One
complex case (sorting recursive tree structures) at first appeared to
need a cmp-function but was found to be simpler and faster using a
key-function.  The net result of the conversation was the feeling that
people who have grown-up using cmp-functions in either Python, C or some
other language feel like they've lost something but really haven't.  In
contrast, people who use SQL or spreadsheet database tools find that key
functions come naturally since neither supports cmp-functions, instead
preferring the user to specify primary and secondary key functions.   

Also, it was pointed-out the removal of cmp-functions in sorted() and
list.sort() was part of a larger effort to remove all forms of cmp from
the whole language (i.e. the builtin cmp function is gone and so it the
__cmp__ magic method).  Rich comparisons have completely supplanted all
uses of cmp-functions in the language as a whole -- having multiple ways
to do it was confusing.

In converting code from 2-to-3, we have found two sticky cases.

The first occurs when an API had exposed cmp functions to the end-user
(for example, unittest.getTestCaseNames() and unittest.makeSuite() have
an optional sortUsing parameter that allows the user to specify a
cmp-function).  To support that use case (so that end-user API's would
not have to be changed), we added a CmpToKey() tool which automatically
converts cmp-functions to key functions.  This tool is referenced in the
docs and it could be added to the 2-to-3 converter.

The second case occurs when a primary key is sorted ascending and a
secondary key is sorted descending.  The technique for that is to take
advantage of sort stability and do two sorts:

   s.sort(key=secondary, reverse=True)
   s.sort(key=primary)   
   # now sorted by primary ascending, secondary descending

That technique is going to be documented in an update of the sorting
how-to.  It doesn't seem to arise much in practice and the cmp function
equivalent seems to be harder for beginners to write (though at least it
can be done with a single cmp-function and a single sort).
History
Date User Action Args
2009-12-05 07:04:50rhettingersetrecipients: + rhettinger, gvanrossum, LeWiemann, tixxit
2009-12-05 07:04:49rhettingersetmessageid: <1259996689.47.0.040805574901.issue1771@psf.upfronthosting.co.za>
2009-12-05 07:04:47rhettingerlinkissue1771 messages
2009-12-05 07:04:45rhettingercreate