Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How would I do the following:


What I have tried:

resp=  'a = 4; print(\"Expected a = 4. Actual a = {} \".format(a))'
rest = exec(resp)
print(rest)
Output:None



How do i store my return value in rest ?
Posted
Updated 16-Aug-22 23:44pm

As explained at Built-in Functions — Python 3.10.6 documentation[^], the return value of the exec function is always None. So your Python script will need to pipe its output into a file which you can then read. Something like:
Python
resp='a = 4; rest = "Expected a = 4. Actual a = {} ".format(a); f = open("foo.txt", "w"); f.write(rest)'
exec(resp)
 
Share this answer
 
Comments
Richard MacCutchan 18-Aug-22 3:52am    
What is the problem?
Richard MacCutchan 18-Aug-22 4:54am    
So what is the problem? And why not use the solution I gave you in my latest comment to your other question at How to capture output from Python interpreter ?[^]?
HelpMewithCode 18-Aug-22 5:13am    
You have given thats worked fine with one command .If its multiple command den its wont work


so tried to used file concept ..write to the file and read again
if write above code about file concept , its just write the txt file , but how do i read from the file
Richard MacCutchan 18-Aug-22 5:18am    
See 7. Input and Output — Python 3.10.6 documentation[^]. You really need to make use of the documentation where everything is explained.
exec has no return value: Built-in Functions — Python 3.10.6 documentation[^] - it always returns None

The code you are executing doesn't even try to return anything. If it did, it would fail:
Quote:
Be aware that the nonlocal, yield, and return statements may not be used outside of function definitions even within the context of code passed to the exec() function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900