0

When I run a program for the second time, how can I make it remember the variables that have changed?

So for the first time it is run:

>>> foo = ["bar", "foo"]
>>> foo[2] = raw_input()

And then the second time it is run it remembers what foo[2] is without the user having to put it in again.

legomyleg
  • 1
  • 1
  • 2
  • You store that variable in a file if you need it in future runs. – Sreeram TP Nov 12 '17 at 04:25
  • what ever values are stored in variables (in any programming language) are stored in the temporary memory (usually RAM) and they do not persist between different program execution cycles. If you want to retain values in such cases, then you need to write the values in the disk by some means like writing to a file or writing to a database. – sid-m Nov 12 '17 at 04:26

2 Answers2

1

You can write data to a file, and read that data back in on subsequent runs. (If the file doesn't exist, or if it is zero length, let the program start from scratch.) This approach is quite practical if you only need to save one or two items of data.

Slightly more generally, persistence is a term commonly used to refer to maintaining data across multiple executions of a program.

See, for example, persistence for Python 2.7 and persistence for Python 3. There, pickling or marshalling data are described. Use functions like dumps() to serialize data for storage, and loads() to de-serialize it. This approach is practical if you save lots of data or if you save complicated data structures.

James Waldby - jwpat7
  • 8,593
  • 2
  • 22
  • 37
0

I don't really understand what you are saying. But if I could guess you have to bind the values you want to certain variables like you do. The second time you run it, it remember the variable's value. Or you can save it to a file and have it there.