[Tutor] reinitializing namespace

Dimitri D'Or dimitri.dor at fssintl.com
Fri Jan 14 09:30:46 CET 2005


Hello Michael,

Thank you for your answer. Actually, my question is not specific to
interactive sessions. I've written a script that loads some modules, create
variables and show figures. What I would like to find, is a command for
clearing all the created variables and close all figures before beginning
the execution of the script. This command will be placed at the beginning of
the script and automatically reinitialize the namespace. The utility of this
command is to guarantee that all the variables that are available in the
namespace after the execution of the script were created by it and are not
remainders from older commands or scripts. Do you think it is possible to do
such a thing?

I'm coming from the Matlab world and I want to find equivalents for the
"clear" and "close" matlab commands.

Thank you for your help,

Dimitri


-----Message d'origine-----
De : Michael Janssen [mailto:mi.janssen at gmail.com] 
Envoyé : jeudi 13 janvier 2005 19:29
À : Dimitri D'Or
Cc : tutor at python.org
Objet : Re: [Tutor] reinitializing namespace

On Thu, 13 Jan 2005 13:20:11 +0100, Dimitri D'Or
<dimitri.dor at fssintl.com> wrote:

> For some purpose, I would like to reinitialize the main namespace, i.e. I
want
> to delete all the variables I have created through the use of functions or
> keyboard entries.

Hello Dimiti,

sound like you're talking about an interactive session. Otherwise
(within a script) it would be a really bad idea to try this (better
put your stuff into functions, that don't create global variables).

Even in an interactive session it sounds like a not that brilliant
idea, especially since I can't think of a way other than using exec
"del %s" % key for appropriate keys from globals(). Finding
"appropiate" keys is one tricky thing.

Why not end your ugly python session and start a new one? You can
define all your imports in the python startfile (on Linux, consult
python manpage. On Windows, I don't know). You can also define useful
functions or variables in your python startfile. This way, you're
really shure that all ugly variables are away without del'iting
anything important.

regards
Michael




More information about the Tutor mailing list