If I define a function
def foo(function):
import inspect
return inspect.getsource(function)
and then call it, I get:
In [11]: foo(lambda x: x[0] + x[1]*2)
Out[11]: 'foo(lambda x: x[0] + x[1]*2)\n'
Note how it printed the entire line, rather than just the lambda function.
Is there a way to get it to output just the lambda?
Desired output:
In [11]: foo(lambda x: x[0] + x[1]*2)
lambda x: x[0] + x[1]*2'
Is there a way to do this that doesn't involve using a regular expression?
EDIT:
Example of how ast.parse(inspect.getsource(function))
may fail:
ast.parse(foo(
lambda x: x+1))