adding save() and load() function for interactive use

Hello, I have a proposal to enhance the usefulness of Python for learning purposes. RFE#852222: 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:
Note that this code is untested. I think this would be a nice addition to Python 2.4. yours, Gerrit. -- 105. If the agent is careless, and does not take a receipt for the money which he gave the merchant, he can not consider the unreceipted money as his own. -- 1780 BC, Hammurabi, Code of Law -- Asperger's Syndrome - a personal approach: http://people.nl.linux.org/~gerrit/english/

Phillip J. Eby wrote:
Ouch. Of course... I should have thought it through better... sorry! yours, Gerrit. -- 219. If a physician make a large incision in the slave of a freed man, and kill him, he shall replace the slave with another slave. -- 1780 BC, Hammurabi, Code of Law -- Asperger's Syndrome - a personal approach: http://people.nl.linux.org/~gerrit/english/

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/)

Phillip J. Eby wrote:
Ouch. Of course... I should have thought it through better... sorry! yours, Gerrit. -- 219. If a physician make a large incision in the slave of a freed man, and kill him, he shall replace the slave with another slave. -- 1780 BC, Hammurabi, Code of Law -- Asperger's Syndrome - a personal approach: http://people.nl.linux.org/~gerrit/english/

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/)
participants (3)
-
Gerrit Holl
-
Guido van Rossum
-
Phillip J. Eby