Raymond Hettinger <rhettin...@users.sourceforge.net> added the comment:
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). ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue1771> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com