-1

Let's say I have a simple python script math_ops.py as follows:

a = 5
b = 4
c = 7
d = a + b
e = b - c
print(d)

I run this script in the Ubuntu terminal as $ python math_ops.py, which prints the value of variable d as 9. Can I somehow get the value of variable e without rerunning the script? It is easy to get the variable values in IDE in such a scenario, but how to retrieve the variables in the terminal?

3

1 Answer 1

0

You can't do this without somehow saving the variable to file.

If you use an interactive interpreter you can run the script in the interpreter and any global variables it creates will still be available to you when the script finishes executing. However, in this case they will still be lost when the interpreter closes.

You should just save them to file. Check out the dump/load functions in the built in json or pickle modules for easily writing/reading small amounts of data to file.

Sign up to request clarification or add additional context in comments.

1 Comment

How can I run math_ops.py in the interactive interpreter? Any suggestions, please.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.