Q: sort's key and cmp parameters
Carsten Haese
carsten.haese at gmail.com
Thu Oct 1 14:26:41 EDT 2009
kj wrote:
>
> Challenge: to come up with a sorting task that cannot be achieved
> by passing to the sort method (or sorted function) suitable values
> for its key and reverse parameters, but instead *require* giving
> a value to its cmp parameter.
Such a task can't exist, because any arbitrary comparison function can
be transformed into a key function. The idea behind the transformation
is to construct wrapper objects that can compare themselves to other
wrapper objects by invoking the given comparison function on their
wrapped originals.
Google for "Hettinger cmp2key" if you want to see the code that does
this transformation.
HTH,
--
Carsten Haese
http://informixdb.sourceforge.net
More information about the Python-list
mailing list