[Python-ideas] a sorting protocol dunder method?

Antoine Pitrou solipsis at pitrou.net
Mon Dec 4 06:02:43 EST 2017

On Sun, 3 Dec 2017 15:06:02 -0800
Chris Barker <chris.barker at noaa.gov> wrote:
> I can't believe this hasn't been brought up before, but searching the web,
> and python-ideas, and all the PEPs has found nothing (could be my lame
> google-fu), so here goes:
> Recent python has moved toward a "key" function for customized sorting:
> list.sort(key=key_fun)
> key is also used (according to
> https://docs.python.org/3.6/library/functools.html#functools.cmp_to_key) in:
> min(), max(), heapq.nlargest(), heapq.nsmallest(), itertools.groupby()
> with this fairly broad use, it seems it's becoming a fairly universal
> protocol for ordering.

+1 from me.

I would also be +1 on an optional class decorator that would generate
all the ordering comparison methods (__lt__, etc.) based on the __key__
method definition.



More information about the Python-ideas mailing list