[Python-ideas] breaking cycles that include __del__

Daniel Stutzbach daniel at stutzbachenterprises.com
Tue Oct 20 15:27:14 CEST 2009

On Tue, Oct 20, 2009 at 8:01 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:

> You don't need __del__, a weakref is good enough since you only need to
> remember
> an adequate string representation of the object:
>    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)

Thank you!  This is a much simpler recipe than the others I have seen.  It
should probably be placed right in the documentation for __del__, as a
suggested alternative.

Having direct support from the garbage collector would still be more memory
efficient (instead of requiring a weakref and a bound function object), but
it will do for many purposes in a pinch.

Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC <http://stutzbachenterprises.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20091020/b8651eec/attachment.html>

More information about the Python-ideas mailing list