[Python-ideas] a sorting protocol dunder method?

brent bejot brent.bejot at gmail.com
Mon Dec 4 13:01:09 EST 2017

I'm +1 on this idea for the most part.

I agree particularly with the idea that it is better OOP for an object to
access it's member variables to create the key than an external container
to do so.

> and then sort like this:
> sorted(list_of_attrdicts, key=AttrDict._id_order)

This is certainly a good pattern to use in the current and older versions,
but I think we can all agree that defining __key__ and calling
"sorted(list_of_attrdicts)" has that syntactic sugar that is

> This will add an additional overhead. This will be even slower than
passing the key function, since you will need to look up the __key__ method
in every item. And there will be an overhead even in the case when the
__key__ method is not defined.

This, to me, is the only possible negative.  I would be most interested to
see how much of an effect this would have on real-world data that doesn't
have __key__ defined.

I may be new to this community but Steven D'Aprano and Antoine Pitrou, you
guys bicker like my parents before they got a divorce.  I'm pretty sure
you're both veterans and so know how to behave yourselves.  Please set the
tone according to how you'd like us newbies to respond.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171204/a43a34bb/attachment.html>

More information about the Python-ideas mailing list