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

Ram Rachum ram.rachum at gmail.com
Fri Apr 12 02:07:18 CEST 2013


Awesome module!


On Fri, Apr 12, 2013 at 2:58 AM, João Bernardo <jbvsmo at gmail.com> wrote:

> You can have something like that with this module I created:
>      https://github.com/jbvsmo/funcbuilder
>
> from funcbuilder import f
> sorted(entries, key=f.datetime_created)
>
>
> Some features in this module are *very* experimental, but are also very
> cool...
>
>
> João Bernardo
>
>
> 2013/4/11 Ram Rachum <ram.rachum at gmail.com>
>
>> Interesting!
>>
>>
>> On Fri, Apr 12, 2013 at 2:33 AM, Haoyi Li <haoyi.sg at gmail.com> wrote:
>>
>>> A more generic and useful thing would be kind of what scala/groovy have:
>>> shorthands for defining function literals:
>>>
>>> Groovy:
>>> myList.sort{it.startTime}
>>>
>>> Scala:
>>> myList.sort(_.startTime)
>>>
>>> Where "_.startTime" and "it.startTime" are shorthand for "x =>
>>> x.startTime" or python's "lambda x: x.startTime". You could probably get
>>> something similar in python:
>>>
>>> sorted(entries, key = x.datetime_created)
>>>
>>> if you did some magic with x to make looking up an attribute return a
>>> lambda that returns that attribute of its argument.
>>>
>>> -Haoyi
>>>
>>>
>>>
>>> On Thu, Apr 11, 2013 at 7:05 PM, Oscar Benjamin <
>>> oscar.j.benjamin at gmail.com> wrote:
>>>
>>>> On 11 April 2013 23:52, 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.
>>>>
>>>> I don't think it's that cumbersome. Leaving aside the import line
>>>> you're only having to specify two things for your key function: that
>>>> it's an attribute (attrgetter) and the name of the attribute
>>>> ('datetime_created'). It's not possible for this to be any more
>>>> succinct without using special case implicit rules which are generally
>>>> a bad thing. I like the fact that the API for the sorted function is
>>>> so simple I can remember all of its arguments and exactly what they do
>>>> without ever needing to look it up.
>>>>
>>>>
>>>> Oscar
>>>> _______________________________________________
>>>> Python-ideas mailing list
>>>> Python-ideas 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
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130412/2b3aeb74/attachment.html>


More information about the Python-ideas mailing list