unloading a module created with imp.new_module

Ian Kelly ian.g.kelly at gmail.com
Mon Nov 24 16:12:34 CET 2014


On Nov 24, 2014 1:27 AM, "Patrick Stinson" <patrickkidd at gmail.com> wrote:
>
> How does the __del__ method have a reference to the module’s globals
dict? because it references the print function?

The module's dict becomes the __globals__ dict used by the function for
looking up globals and builtins.

> Crazy. Is there any other way to comfort when a module is being deleted
beside defining a class object with a printing dtor?

Modules don't strictly have to be ModuleType. You could substitute a class
with a __del__ method for the module itself for testing this. Be careful
with leaving this in your production code though, as the presence of
__del__ methods can interfere with garbage collection and are not advisable
prior to 3.4.

You can also test whether an object is still in memory by looking for it in
gc.get_objects() although be warned that is an expensive call.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141124/f9ad5908/attachment.html>


More information about the Python-list mailing list