[Python-ideas] a sorting protocol dunder method?

Nathan Schneider neatnate at gmail.com
Sun Dec 3 18:46:45 EST 2017


On Sun, Dec 3, 2017 at 6:06 PM, Chris Barker <chris.barker at noaa.gov> wrote:

> In fact, it's striking me that there may well be classes that are defining
> the comparison magic methods not because they want the objects to "work"
> with the comparison operators, but because that want them to work with sort
> and min, and max, and...
>

An existence proof: in NLTK, an __lt__ method added purely to facilitate
consistent sorting (in doctests) of structured data objects for which
comparison operators do not really make conceptual sense:
https://github.com/nltk/nltk/pull/1902/files#diff-454368f06fd635b1e06c9bb6d65bd19bR689

Granted, calling min() and max() on collections of these objects would not
make conceptual sense either. Still, __sort_key__ would have been cleaner
than __lt__.

Cheers,
Nathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171203/af7983da/attachment.html>


More information about the Python-ideas mailing list