[Python-Dev] any support for a methodcaller HOF? (original) (raw)

Alex Martelli aleaxit at gmail.com
Thu Feb 2 23:43:51 CET 2006


I was recently reviewing a lot of the Python 2.4 code I have written, and I've noticed one thing: thanks to the attrgetter and itemgetter functions in module operator, I've been using (or been tempted to use) far fewer lambdas, particularly but not exclusively in key= arguments to sort and sorted. Most of those "lambda temptations" will be removed by PEP 309 (functional.partial), and most remaining ones are of the form: lambda x: x.amethod(zip, zop)

So I was thinking -- wouldn't it be nice to have (possibly in module functional, like partial; possibly in module operator, like itemgetter and attrgetter -- I'm partial to functional;-) a methodcaller entry akin to (...possibly with a better name...):

def methodcaller(methodname, *a, **k): def caller(self): getattr(self, methodname)(*a, **k) caller.name = methodname return caller

...? This would allow removal of even more lambdas.

I'll be glad to write a PEP, but I first want to check whether the Python-Dev crowd would just blast it out of the waters, in which case I may save writing it...

Alex



More information about the Python-Dev mailing list