Q: sort's key and cmp parameters

Paul Rubin http
Tue Oct 6 04:34:27 CEST 2009

Raymond Hettinger <python at rcn.com> writes:
> There were two sorts in my post and only one in yours.
> That's why you didn't get the same answer.

Whoops, missed that.

> When Guido made the final call, I'm sure he was balancing
> a number of goals including language simplification and
> one-way-to-do-it.  I'm also sure that sorting lazily
> evaluated infinite sequences was not on his radar screen :-)

I can't help wondering if there will be feature requests for a cmp
argument for the rest of eternity until it eventually makes its way
back in.

> Psychologically, the thing that I find to be interesting is
> that beginners and intermediate users seem to take to key
> functions more readily than old timers.

I think key is preferable in at least 90% of the cases.  I also think
a general purpose language should be able to handle 100% of the cases,
so if key is all you have, there can be a gap to fill.

> In contrast, some people who have have had deep experience with
> cmp functions may tend to first think of cmp solutions and then
> have a harder time seeing solutions with key functions.  If you
> grew-up on C's qsort() like I did, then a key function may not
> be the first thing that pops into your head.

That could be.  

> One other interesting data point:  Python uses key functions
> in min(), max(), heapq.nsmallest(), heapq.nlargest, and
> itertools.groupby().  Those functions never supported a
> cmp function argument, nor has one ever been requested.

Interesting.  Maybe someday...

More information about the Python-list mailing list