Lifetime Of Static Class Members/class References?
Solution 1:
Module globals are cleaned up on Python exit, and your class reference is already gone by the time the __del__
hook is run.
Don't count on globals still being there. Rather, use type(self)
to get the class reference:
def __del__(self):
print "destructing", self.name
cls = type(self)
if not cls._finalised.get(self.name):
print "first destruction"
self.logger.info('** My Epilogue **')
cls._finalised[self.name] = True
This is documented in the big Warning section on the object.__del__
hook documentation:
Also, when
__del__()
is invoked in response to a module being deleted (e.g., when execution of the program is done), other globals referenced by the__del__()
method may already have been deleted or in the process of being torn down (e.g. the import machinery shutting down). For this reason,__del__()
methods should do the absolute minimum needed to maintain external invariants.
Take into account that module globals are maintained in a dictionary, and thus the order in which they are cleared is subject to the current implementation-and-Python-version-dependent order of the dictionary when being cleared.
Post a Comment for "Lifetime Of Static Class Members/class References?"