On Apr 11, 2013, at 6:52 PM, Ram Rachum
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...@python.org http://mail.python.org/mailman/listinfo/python-ideas _______________________________________________ Python-ideas mailing list Python-ideas@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