[Python-ideas] Allow key='attribute_name' to various sorting functions

Ram Rachum ram.rachum at gmail.com
Fri Apr 12 00:24:00 CEST 2013


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')

The `sorted` function can check whether the `key` argument is a string, and 
if so do an attribute lookup.

Since I see no other possible use of a string input to `key`, I don't see 
how this feature would harm anyone.

What do you think?


Thanks,
Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130411/584fcaec/attachment.html>


More information about the Python-ideas mailing list