I have python functions in string format and I want to get the python objects of these functions in the scope of the program. I have tried exec()
, eval()
and ast.literal_eval()
but none of these return a function object.
For example:
s = "def add(args):\n try:\n return sum([int(x) for x in args])\n except Exception as e:\n return 'error'\n
this is a simple function in string to add elements of a list. I am looking for a module of utility which can return the object of the function add
function_obj = some_function(s)
print 'type:', type(function_obj)
type: <type 'function'>