unloading imported modules

Gerhard Häring gerhard.haering at gmx.de
Sat Jul 20 16:46:33 EDT 2002


* François Pinard <pinard at iro.umontreal.ca> [2002-07-20 16:26 -0400]:
> [Alex Martelli]
> 
> > xeon wrote:
> 
> > > Is it possible in python intepreter to unload imported modules? or are
> 
> > No.
> 
> Hello, Alex.
> 
> Suppose we reload, and reload and reload the same module, are the
> previous copies freed from memory?

The previous copies of what? As everything is an object in Python,
whatever was in the module gets garbage collected as soon as all
references to it are gone. Maybe an example can demonstrate where and
why this happens:

>>> import foo
>>> x = foo.Foo()
>>> x.__class__
<class foo.Foo at 0x810df64>
>>> 
[1]+  Stopped                 python
gerhard at lilith:/tmp$ vim foo.py

[slightly change foo.Foo here]

gerhard at lilith:/tmp$ fg
python

>>> reload(foo)
<module 'foo' from 'foo.py'>
>>> y = foo.Foo()
>>> y.__class__
<class foo.Foo at 0x810f52c>
>>> del x # <--- Here the old foo.Foo class gets garbage-collected
>>>       #      as the last reference to it will go away with x

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))





More information about the Python-list mailing list