[Tutor] How to save updated values in a program?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Sep 1 21:15:42 CEST 2005

On Thu, 1 Sep 2005, Nathan Pinno wrote:

> How do I save updated values in a program when it quits? Are the new
> values automatically saved, or do I have to code it in to the program in
> order to save the updated values?

Hi Nathan,

We'll have to do something explicit to save them.

In the past, some computer systems had a notion of saving the "image" of
the whole working environment.  From what I understand, the Smalltalk
language did something like this.  By having a notion of saving this
environmental "image", these systems made it very easy to restore the
state of the system.  We just restore the environment image, and all our
variables came back to life.

But, on the flip side, it made it very difficult to distribute programs to
other people: it became difficult to disentangle the parts of the
environment that dealt with the program from the rest of the system.

So for the most part, the rest of the computing industry went the other
direction, toward not saving anything unless explicitely saved it in files
or databases.  And that's the state of things in Python.

Take a look at the 'shelve' module:


It's a module that provides a dictionary-like object: that dictionary
object is actually tied to disk, so anything that goes into the dictionary
gets saved.  Try it out, and if you have questions with it, please feel
free to bring it up onto the list.

Good luck!

More information about the Tutor mailing list