[Python-Dev] operator.methodcaller

James Y Knight foom at fuhm.net
Fri Mar 11 23:48:02 CET 2005


On Mar 11, 2005, at 2:02 PM, Steven Bethard wrote:
> On Fri, 11 Mar 2005 19:48:45 +0100, Alex Martelli <aleaxit at yahoo.com> 
> wrote:
>> Which reminds me -- could we have a methodcaller relative to 
>> attrgetter
>> and itemgetter?  "Sort a list of strings in a case-insensitive way"
>> would become *SO* easy with sort(dalist, key=methodcaller('lower'))...
>> can't REALLY recommend sort(dalist, key=str.lower) then the items of
>> dalist MIGHT be either str or unicode
>
> I'd like to second this suggestion -- I've run into this problem a few
> times.  When you're using a listcomp or genexp, you can inline it of
> course, but especially with a lot of functions growing the incredibly
> convenient key= arguments in 2.5 (e.g. min, max and the deque
> functions if I recall correctly), methodcaller would be a much more
> duck-typing friendly way to create such callables.

Yeah, if *only* we had some way to create callables in a nice, short, 
generic, and easy to understand way.

How about this proposal:
 >> sort(dalist, key=lambda x: x.lower())

Who wants to make a PEP for it? Maybe we can add it in Python 3000.

James



More information about the Python-Dev mailing list