Module magic methods

Michael Hudson mwh21 at cam.ac.uk
Sun Jun 13 12:11:05 EDT 1999


arcanedragonNOSPAM at NOSPAMhome.com (Chris Herborth) writes:

> Do modules have magic methods?  Specifically, I'm wondering if there's a
> __del__() or __destroy__() method that I could use to clean up after myself
> a little when the module is about to be unloaded...

Don't think so. However you can fake it by creating a class instance
and storing it in a module global, like so:

class ByeBye:
    def __del__(self):
        print "bye bye"

_byebye = ByeBye()

Then, so long as no other entity has a reference to _byebye, when the
module is unloaded _byebye will be collected and the __del__ method
called.

You know that, in general, modules aren't unloaded, except at
interpreter shutdown time? And then the execution state is so
unpredictable you can't really do anything?

HTH
Michael

FWIW, I've often thought it would be funky to be able to define
__add__ or __call__ etc methods to modules. Pointless, I'd have to
admit, but still funky.
 
> -- 
> ----------================================================----------  _
> Chris Herborth, R&D, Technical Writing                               | \  _
> Arcane Dragon Software                 Arcane Dragon -==(UDIC)==-    | < /_\
> arcane dragon at home dot com          DNRC Holder of Past Knowledge |_/ \_




More information about the Python-list mailing list