How to clean a module?
Diez B. Roggisch
deets at nospam.web.de
Thu May 31 11:21:08 EDT 2007
ai schrieb:
> It assumes that there is a module A which have two global variables X
> and Y. If I run "import A" in the IDLE shell, then I can use A.X and
> A.Y correctly. But if I want to change the module A and then delete
> the variable Y, I find I can use A.Y just the same as before!
> In fact, I have tried all the following methods but can't remove the
> A.Y:
> execute "import A" again
> "reload(A)"
> "del A; import A"
> Yes, if you use "del A.Y", it works. But it is stupid since there are
> probably many names. In my thought, if no one references objects in A,
> "del A" will release all memory about A. But it seems that the fact is
> not. So I can not refresh the namespace to follow changes of a module
> easily and I will worry about the memory if I del a module.
> I want to know if there is a way to clear a module entirely.
There might be other answers - but the easiest and IMHO best is to
simply restart the interpreter. Because whatever you type in there, you
could or should even (if it reaches some complexity) put in a small test
script - and execute that from the interpreter at a shell prompt. The
advantage is that you don't suffer from any side-effects e.g. IDLE has
(no Tk mainloop for example) and avoid the problems you describe
entirely. Together with a bunch of others.
If you want/have to, you can drop into interpreter mode after script
execution with
python -i myscript.py
Diez
More information about the Python-list
mailing list