[Python-Dev] adding save() and load() function for interactive use

Guido van Rossum guido at python.org
Mon Dec 1 14:07:11 EST 2003

>  I think that for learning Python, it would be nice to
>  save the current namespace to a file using a builtin
>  save() function (and corresponding load()). For
>  beginning programmers, it would be nice to be able to
>  tinker interactively with the shell, defining functions
>  and maybe even classes, with which they can continue
>  later on. save() is easily implemented by pickling
>  locals(); load() is easily implemented by updating
>  locals() while unpickling it. The functions would take
>  a filename (string) as an argument. In Idle, they could be
>  in the menu. Like the _ variable, load() and save() are
>  only present in the interactive shell (some global
>  .pythonrc?). Example usage:

Saving classes and functions with pickle doesn't work; it *appeared*
to work only because you didn't actually quit your session.

IDLE has a feature designed to further the same goal without having to
save a "workspace" (a vague concept at best and one that doesn't scale
to larger Python apps).  You can configure IDLE to come up with a
blank untitled Python file instead of a Python Shell window.  Then the
student's work cycle is intended to be as follows:

  - Type some code into the file.

  - Save it (the first time the student has to pick a filename).

  - Hit F5 which will run the file (which is now a module); this will
    open a Python Shell (if one isn't already open) so you can
    experiment with the code defined in the file, but these
    experiments won't be saved.

  - Go back to the top to edit the file and run it again.

There's also an option to avoid the save step (F5 will save
automatically once a filename is picked).

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list