[Tutor] reinitializing namespace

Kent Johnson kent37 at tds.net
Fri Jan 14 12:02:36 CET 2005


I think you may be looking for something that is not needed in Python or that you can easily do 
another way.

If you are running a script from the command line, e.g.
  > python myscript.py

then myscript.py will have a completely fresh runtime environment every time you call it.

If you are running the script by importing in another module, then you can use reload() to reload 
the imported script. That will reinitialize the namespace of the module, which for your purposes 
*is* the global namespace of the module.

Please give us more details about how you will run the script and what kind of problem you anticipate.

Kent

Dimitri D'Or wrote:
> 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
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list