Q: sort's key and cmp parameters

kj no.email at please.post
Thu Oct 1 18:16:13 EDT 2009


In <7x1vln2bzh.fsf at ruckus.brouhaha.com> Paul Rubin <http://phr.cx@NOSPAM.invalid> writes:

>kj <no.email at please.post> writes:
>> Is there a real-life sorting task that requires (or is far more
>> efficient with) cmp and can't be easily achieved with key and
>> reverse?

>Yes, think of sorting tree structures where you have to recursively
>compare them til you find an unequal pair of nodes.  To sort with
>key=... you have to wrap a class instance around each tree, with
>special comparison methods.  Blecch.


Good point.  This example convinces me that it was a bad idea to
get rid of cmp in Python 3, even if situations like this one are
rare.  With the cmp parameter as an option, coding this type of
sort was accessible even to those with a rudementary knowledge of
Python.  Now one needs to be pretty clever and pretty skilled with
Python to figure this one out...  Granted, anyone who needs to
perform such sorts is probably smart enough to handle the required
solution, but not necessarily very proficient with Python.  Besides,
why make something that was relatively straightforward before become
an exercise in cleverness?  I wonder what was gained by eliminating
the cmp option...

kj



More information about the Python-list mailing list