on sorting things

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Dec 19 02:49:21 EST 2019


On Thu, 19 Dec 2019 at 07:37, Eli the Bearded <*@eli.users.panix.com> wrote:
>
...
> But what caught my eye most, as someone relatively new to Python but
> with long experience in C in Perl, is sorting doesn't take a
> *comparison* function, it takes a *key generator* function, and that
> function is supposed to transform the thing into something that the
> native comparison knows how to compare.
>
> This seems a strange choice, and I'm wondering if someone can explain
> the benefits of doing it that way to me.
>
> Elijah
> ------
> imagines it could make porting some code in or out of Python trickier

Python 2 used comparison functions but Python 3 uses key functions.
There is a helper called cmp_to_key that can convert a comparison
function into a key function:
https://docs.python.org/3.8/library/functools.html#functools.cmp_to_key

--
Oscar


More information about the Python-list mailing list