Guido rethinking removal of cmp from sort method

Paul Rubin at nospam.invalid
Fri Apr 1 21:22:59 CEST 2011

Terry Reedy <tjreedy at> writes:
>> What happens then is you define a new interface.
> Like key= versus cmp=

Well, in an untyped language like Python, adding a feature to an
interface doesn't require defining a new interface unless you change
something incompatibly.  key= is very useful but it can be added without
breaking cmp= .

> 2to3 could probably gain a fixer to change
> .sort(cmp=f) # to
> import functools import cmp_to_key
> .sort(key=functools.cmp_to_key(f))
> I know some would not like this because interface change is not their
> real concern.

Looks like a good idea.  There is an efficiency hit from the above in
some situations, but at least it prevents code from breaking, so unless
there's some drawback I'm not currently spotting, it's better than
nothing and I'd endorse adding such a wrapper.  2to3 should show some
kind of diagnostic and maybe put a comment into the output code,
when it does that particular transformation, since most of the
time there's probably a better way to write the key function.

More information about the Python-list mailing list