How is max supposed to work, especially key.

Terry Reedy tjreedy at udel.edu
Thu Dec 4 10:47:21 EST 2014


On 12/4/2014 5:35 AM, Albert van der Horst wrote:

> I agree that it is a useful function and that it is doing
> the right thing. What is wrong is the name.
> I refer to the fact that it is not returning the maximum.
> It returns the iterator value that leads to the maximum.
> A function that doesn't return a maximum shouldn't be called
> maximum.

The key function serves the same purpose as with sort.  It define a 
custom ordering of items, not the values returned.  'x < y' is defined 
as key(x) < key(y), where the comparison of keys uses the standard (and 
for numbers, natural) built-in ordering.  So max returns the max value 
according to the order you define with the key function.  The default 
key is the identity function, so the default order is the builtin order.

-- 
Terry Jan Reedy




More information about the Python-list mailing list