Re: How to clean a module?



ai a écrit :
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!

It's unlikely to be true, see below.

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.


Actually I do not see your problem and your exact need, when I type the following in python prompt I just see expected behavior, what is a problem to you ? Maybe you could post a code explaining it.


In [64]: import a



In [65]: a.X

Out[65]: 0



In [66]: a.X = 2



In [67]: del a



In [68]: import a as b



In [69]: b.X

Out[69]: 2



In [71]: for name in [ n for n in b.__dict__ if not n.startswith('__') ] :
....: b.__dict__.__delitem__(name)

....:

....:



In [72]: b.X

---------------------------------------------------------------------------

<type 'exceptions.AttributeError'> Traceback (most recent call last)


C:\Documents and Settings\maric\Bureau\<ipython console> in <module>()



<type 'exceptions.AttributeError'>: 'module' object has no attribute 'X'



In [73]: reload(b)

Out[73]: <module 'a' from 'a.pyc'>



In [74]: b.X

Out[74]: 0


In [75]: del b.X



In [76]: del b



In [77]: import a



In [78]: a.b

---------------------------------------------------------------------------

<type 'exceptions.AttributeError'> Traceback (most recent call last)


C:\Documents and Settings\maric\Bureau\<ipython console> in <module>()



<type 'exceptions.AttributeError'>: 'module' object has no attribute 'b'



.



Relevant Pages

  • Re: Deleting objects on the fly
    ... | del x will remove x from memory if nothing else is refering to it, ... In that language memory is allocated until a request cannot be satisfies, then a scan is performed for unreferenced objects whose space is reclaimed. ... Only if this doesn't yield enough space to allocate the new object is more memory requested from the OS by the process. ...
    (comp.lang.python)
  • Re: Python memory handling
    ... thanks you for your explanations about memory handling in the ... I've tried with python 2.5 under linux: ... del et #43.3 Mb used ...
    (comp.lang.python)
  • How to clean a module?
    ... It assumes that there is a module A which have two global variables X ... Yes, if you use "del A.Y", it works. ... easily and I will worry about the memory if I del a module. ...
    (comp.lang.python)
  • Re: How to clean a module?
    ... could I infer that the statement "del xxx" ... doesn't release the memory which xxx used? ... script - and execute that from the interpreter at a shell prompt. ...
    (comp.lang.python)
  • Re: Deleting objects
    ... > all of my memory back, ... > del db ... between name db and the database object. ... CPython will 'free' memory immediately (and ...
    (comp.lang.python)