Q: sort's key and cmp parameters

Paul Rubin http
Tue Oct 6 14:40:46 EDT 2009


Bearophile <bearophileHUGS at lycos.com> writes:
> The problem is that if you allow to use the cmp, lot of programmers
> will use it straight away, not even bothering to know what that
> strange 'key' argument may be useful for. And they miss the how much
> handy 'key' is.

Given how often we hear "consenting adults" as justification for any
number of gaps in Python error checking, the argument above is
singularly unpersuasive.

> I am having a very hard type (despite my implementation, explanations,
> etc) explaining to D programmers why for a flexible general-purpose
> function a key function argument is often better. They in the end have
> added something like that in the std lib, but with a weird name like
> SchwartzSort that's surely not the first standard sort they look
> for... this is bad.

"key" and the DSU (Schwartz) sorting pattern makes lots of sense in
scripting languages like Python that are primarily used on relatively
small data sets.  The best reason for writing something in a lower
level language like D is because you want to push the limits of your
availiable machine resources.  Therefore, the DSU strategy's extra
memory consumption will be what you want less of the time than it is
in Python.



More information about the Python-list mailing list