[Python-ideas] breaking cycles that include __del__

Daniel Stutzbach daniel at stutzbachenterprises.com
Tue Oct 20 18:58:36 CEST 2009


I'm playing around with this, and in following variant the weakref callback
is never called (in Python 2.6 and 3.1).  I do not see why.  I know I've
created a cycle between the callback and the object itself, but the garbage
collector should detect and free the cycle.

import weakref, gc
class Foo:
    def __init__(self):
        self._weakref = weakref.ref(self, self.__free__)
    def __free__(self):
        print("I'm free!")
x = Foo()
del x
gc.collect()
print('test')
print(gc.garbage)


--
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/298f462e/attachment.html>


More information about the Python-ideas mailing list