Earlier today I asked this question about the
As we told you before, you really shouldn't rely on
__del__ being called when the interpreter exits. There are two options for doing this properly:
The first is
import os import atexit class Logger(object): def del(self): print "os: %s." % os logger = Logger() atexit.register(logger.del)
This makes sure that your logger gets finalized at exit.
*Reading a little more into your problem, since you plan on having a single instance bound to a module which defines the instances's class, the context manager solution below won't work for this since there's no way to stay in the context for the entire execution of your program. You'll need to use
atexit.register. However, from the standpoint of program design, I would much prefer to use a context manager to manage my resources than
atexit.register if restructuring the code would allow it.
(better*) way to do it is make your class a context manager which executes the cleanup code when you exit the context. Then your code would look like:
import os class Logger(object): def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): print "os:",str(os) with Logger() as logger: #do something ...