dynimac code with lambda function creation
Justin Delegard
justdelegard at gmail.com
Thu Mar 27 10:19:51 EDT 2008
So I am trying to pass an object's method call to a function that
requires a function pointer. I figured an easy way to do it would be to
create a lambda function that calls the correct method, but this is
proving more difficult than I imagined.
Here is the function I'm using:
def objectMethodCallerFunctionCreator(testerObj, method):
exec("y=lambda x: testerObj."+method+"(x)")
return y
Where testerObj is an instance of an object, and method is a string
representing the method to call. The problem is, when I actually run
the function created (y in this case), it tells me it can't find the
symbol testerObj in the global scope. I have successfully created
similar functions, except without using the exec() call. e.g.
def functionCreator(a, b, c):
return lambda d: myFunc(a, b, c, d)
and it doesn't complain about the variables a, b, or c when being run.
I am assuming this is happening because of the exec() call, but I don't
see another way of calling a variable method on an object. Is there
some other way to do this that I'm missing? I tried passing in 'method'
as a function pointer (to the method of the object), but that didn't
work either.
Thanks,
Justin
More information about the Python-list
mailing list