Module magic methods

Greg Ewing greg.ewing at compaq.com
Wed Jun 16 19:15:08 EDT 1999


"G. David Kuhlman" wrote:
> 
>     import mymodule
>     #
>     # Do some stuff with mymodule
>     #
>     mymodule = None
> 
> Won't reference counting cause the space taken up by mymodule to be
> garbage collected?

Nope, because references all imported modules are kept
on a global list in the bowels of the interpreter, so
that the next time the module is imported you get a
reference to the previously loaded instance.

> this is what
> I'm going to tell them to do when they want to reclaim space in
> long running programs.

It won't work.

>     anInstance = mymodule.MyClass()
>     mymodule = None
>     anInstance.show()
> 
> Something must stay around so that I can call the methods defined
> MyClass.

The instance has a reference to the class, so as long as the
instance is alive the class will be too, even if you wipe
out the reference to it in the module where it was defined.

Greg




More information about the Python-list mailing list