anonymous functions/expressions without lambda?

Michael Hoffman cam.ac.uk at mh391.invalid
Thu Apr 28 15:56:26 EDT 2005


Paul Miller wrote:

> While on the subject, is there an equivalent for "methodcaller"?
> 
> ie. if I want to bind a function which calls a specific method of an 
> object with a specific parameter?
> 

def funccaller(func, *args, **kwargs):
     def _return_func():
         return func(*args, **kwargs)
     return _return_func

class Test1(object):
     def __init__(self, x):
         self.x = x
     def method(self, a, b, c):
         return self.x + a + b + c

t1 = Test1(42)

funccaller_result = funccaller(t1.method, 3, 4, c=5)
funccaller_result()

And this time I actually tested it, and it works! ;)
-- 
Michael Hoffman



More information about the Python-list mailing list