Say I have python code in line
code = """ a = 42 a """
and I exec this line of code:
result = exec(code)
Then result will always be None . Is there any way to get an estimate of the last expression? In this case, it will be 5 , since a was the last expression.
EDIT: Here is another example of the functionality I'm asking about. Say we have python code (stored in the code variable)
a = 100 sqrt(a)
Then how can I execute the code in such a way as to give me a result of 10 - i.e. sqrt(a) ?
EDIT EDIT: Another example: the code I want exec ,
function_a() function_b() function_c()
Is there any way to define any magic_exec function magic_exec that
magic_exec(code)
will provide me the value of function_c() ?
source share