[Python-Dev] operator.methodcaller

Steven Bethard steven.bethard at gmail.com
Fri Mar 11 20:02:53 CET 2005

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 items...

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.

You can wordify anything if you just verb it.
        --- Bucky Katt, Get Fuzzy

More information about the Python-Dev mailing list