[Python-ideas] a sorting protocol dunder method?

Nathaniel Smith njs at pobox.com
Mon Dec 4 04:16:26 EST 2017

On Sun, Dec 3, 2017 at 10:48 PM, Carl Meyer <carl at oddbird.net> wrote:
> It'd be nice to be able to eliminate an import and have the lines of
> code and instead write that as:
>     class BankAccount:
>         def __init__(self, balance):
>             self.balance = balance
>         def __sort_key__(self):
>             return self.balance

What if we added a @key_ordering decorator, like @total_ordering but
using __key__ to generate the comparisons? I know you'd have to do an
import, but usually adding things to the core language requires more
of a benefit than that :-).


Nathaniel J. Smith -- https://vorpus.org

More information about the Python-ideas mailing list