[Python-ideas] breaking cycles that include __del__
Antoine Pitrou
solipsis at pitrou.net
Tue Oct 20 15:01:31 CEST 2009
Daniel Stutzbach <daniel at ...> writes:
>
> Unfortunately, if the object ends up in a cycle, then the recipe can't do
> its job because __del__ will never be called.
You don't need __del__, a weakref is good enough since you only need to remember
an adequate string representation of the object:
class Widget:
closed = False
def __init__(self):
# ...
def error_not_closed(_, r=repr(self)):
log_an_error('%s not closed properly' % r)
self._wr_not_closed = weakref.ref(self, error_not_closed)
def close(self):
if self.closed: return
self.closed = True
# Destroy weakref
self._wr_not_closed = None
# Free various resources from ctypes, etc.
Regards
Antoine.
More information about the Python-ideas
mailing list