I'm still not sure why there is a problem. How are you running the scripts from the interactive session? If you are importing the script, its variables will be in its own namespace. If you are creating variables interactively yourself, your best bet is probably to just restart the interpreter. (If you use IDLE you can reset the interpreter from a menu without having to quit IDLE.)

It sounds like you are just coming to Python from Matlab and you are anticipating the same kinds of problems in Python as you are used to in Matlab. Have you actually worked with a script in the interpreter? Can you show us a sample interaction? Are you having a problem with name collisions in Python?

It really sounds to me like this is either a non-issue or that you should just restart the interpreter when you need a clean slate.

Here is an example of isolation between modules and reloading.

Suppose A1.py and A2.py both contain this code (except A2 prints 'A2.a ='):

## A1.py
a = 0

def bumpA():
  global a
  a += 1
  print 'A1.a =', a


I can use these modules interactively like this: >>> import A1 >>> A1.bumpA() A1.a = 1 >>> A1.bumpA() A1.a = 2 >>> A1.bumpA() A1.a = 3 >>> A1.bumpA() A1.a = 4

 >>> import A2
 >>> A2.bumpA()
A2.a = 1
 >>>
 >>> A2.bumpA()
A2.a = 2

Notice that A1.a and A2.a are different - each module has its own namespace, 
there is no collision.

If I reload a module it gets reset to its initial state:
 >>> reload(A2)
<module 'A2' from 'A2.pyc'>
 >>> A2.bumpA()
A2.a = 1

Kent

Dimitri D'Or wrote:
Dear Kent,

Consider I'm working with an interactive session during which I have already
run some scripts. Those scripts have produced several variables, say, e.g.,
a and b. Now I execute myscript which also creates variables named a and b,
but with a possibly different type or content. To be sure that the variables
I find in my workspace after the execution of myscript were created by this
script and not by another one, I want to reset the workspace.

Otherwise, how can I make a difference between variables having the same
name but created by two different scripts?

Maybe this question is too much related to my Matlab experience and not
relevant into Python's philosophy. Please tell me what I should do.

Dimitri

-----Message d'origine-----
De : [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] De la part de Kent
Johnson
Envoyé : vendredi 14 janvier 2005 12:03
Cc : tutor@python.org
Objet : Re: [Tutor] reinitializing namespace

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:[EMAIL PROTECTED] Envoyé : jeudi 13 janvier 2005 19:29
À : Dimitri D'Or
Cc : tutor@python.org
Objet : Re: [Tutor] reinitializing namespace


On Thu, 13 Jan 2005 13:20:11 +0100, Dimitri D'Or
<[EMAIL PROTECTED]> 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@python.org http://mail.python.org/mailman/listinfo/tutor



_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor




_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Reply via email to