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

Bengt Richter bokr at oz.net
Fri Feb 3 18:51:31 CET 2006


On Thu, 2 Feb 2006 14:43:51 -0800, Alex Martelli <aleaxit at gmail.com> wrote:

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. Yes, but what semantics do you really want? The above, as I'm sure you know, is not a direct replacement for the lambda:

import dis foo = lambda x: x.amethod(zip, zop)

def methodcaller(methodname, *a, **k): ... def caller(self): ... getattr(self, methodname)(*a, **k) ... caller.name = methodname ... return caller ... bar = methodcaller('amethod', zip, zop) Traceback (most recent call last): File "", line 1, in ? NameError: name 'zop' is not defined zop = 'must exist at methodcaller call time' bar = methodcaller('amethod', zip, zop) dis.dis(foo) 1 0 LOAD_FAST 0 (x) 3 LOAD_ATTR 1 (amethod) 6 LOAD_GLOBAL 2 (zip) 9 LOAD_GLOBAL 3 (zop) 12 CALL_FUNCTION 2 15 RETURN_VALUE dis.dis(bar) 3 0 LOAD_GLOBAL 0 (getattr) 3 LOAD_FAST 0 (self) 6 LOAD_DEREF 2 (methodname) 9 CALL_FUNCTION 2 12 LOAD_DEREF 0 (a) 15 LOAD_DEREF 1 (k) 18 CALL_FUNCTION_VAR_KW 0 21 POP_TOP 22 LOAD_CONST 0 (None) 25 RETURN_VALUE

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... -0 ;-)

Regards, Bengt Richter



More information about the Python-Dev mailing list