key argument for max?

Raymond Hettinger python at rcn.com
Sun Oct 24 01:50:58 EDT 2004


[Steven Bethard]
> > I was wondering if there's any plans to add a "key" argument to max
> > (and min) like was done for sort(ed)?  I fairly often run into a

[Alex Martelli]
> Unfortunately, no.  I think that the excellent key= optional argument to
> sort should be extended to every order-related Python tidbit; min and
> max are examples, but so should the heapq module's functions.
> Unfortunately, this idea wasn't liked by the python-dev consensus back
> in 2.4's formative stages, and it's now too late for any functional
> changes to 2.4.  So, we can take it up again for 2.5 at some time.

It should definitely be pursued for Py2.5.   I'm the one who shot it
down the first time it came up and now wish that it had gotten a full
hearing.

The issue was political.  Getting a key= argument for sort() was a big
win; however, proposing to propagate the idea could have jeopardized
everything by arousing the ire of the gathering anti-change movement.



Raymond Hettinger


P.S.  The heapq module's new nsmallest() and nlargest() functions are
suitable for the key= argument; however, the other functions are not. 
Unfortunately, the heap is not a collection object in its own right. 
I don't see a way to attach an ordering function or a reversed=
argument for the whole heap.  Such arguments would have to be passed
on every call to heapify(), heappop(), heappush(), and heapreplace().



More information about the Python-list mailing list