[Python-ideas] a sorting protocol dunder method?

Serhiy Storchaka storchaka at gmail.com
Mon Dec 4 09:00:17 EST 2017

04.12.17 14:25, Steven D'Aprano пише:
> On Mon, Dec 04, 2017 at 08:45:55AM +0200, Serhiy Storchaka wrote:
>> But the idea of the class decorator looks more sane to me.
> The purpose of __key__ is to define a key function (not a comparison
> operator) for classes that aren't orderable and don't have __lt__.
> If you're going to then go ahead and define __lt__ and the other
> comparison operators, there's no point to __key__.

Right. The only benefit of this decorator is that it could avoid writing 
a boilerplate code for simple cases. Just add 
@ordered_by_key(attrgetter('name', 'weight')).

__key__ is not needed, just pass the key function as an argument of the 

Of course if it can be useful this doesn't mean that it should be 
included in the stdlib. It could live on PyPI.

More information about the Python-ideas mailing list