[Python-ideas] Allow key='attribute_name' to various sorting functions
Donald Stufft
donald at stufft.io
Fri Apr 12 00:54:50 CEST 2013
On Apr 11, 2013, at 6:52 PM, Ram Rachum <ram.rachum at gmail.com> wrote:
> On Friday, April 12, 2013 1:35:20 AM UTC+3, Carl Meyer wrote:
> On 04/11/2013 04:24 PM, Ram Rachum wrote:
> > I often want to sort objects by an attribute. It's cumbersome to do this:
> >
> > sorted(entries, key=lambda entry: entry.datetime_created)
> >
> > Why not allow this instead:
> >
> > sorted(entries, key='datetime_created')
>
> from operator import attrgetter
> sorted(entries, key=attrgetter('datetime_created'))
>
> You can alias attrgetter to an even shorter name if you like.
>
> That's still cumbersome in my opinion.
>
>
> Explicit utility functions are better than implicit special-case
> behaviors. Why should a string be special-cased to attribute lookup
> rather than, say, __getitem__ lookup?
>
> Right, these are options too. I'd guess that attribute lookup is more common, but maybe I'm wrong.
>
>
> Carl
> _______________________________________________
> Python-ideas mailing list
> Python... at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
Special cases aren't special enough to break the rules.
-----------------
Donald Stufft
PGP: 0x6E3CBCE93372DCFA // 7C6B 7C5D 5E2B 6356 A926 F04F 6E3C BCE9 3372 DCFA
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130411/b7f0a025/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 841 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130411/b7f0a025/attachment.pgp>
More information about the Python-ideas
mailing list