__del__ doesn't work
Graham Dumpleton
grahamd at dscpl.com.au
Sun Feb 15 19:49:51 EST 2004
PeterAbel at gmx.net (Peter Abel) wrote in message news:<21064255.0402151141.51a38fed at posting.google.com>...
> I have an application, which is an instance of a class
> with a deeply nested object hierarchy. Among others one
> method will be executed as a thread, which can be stopped.
> Everything works fine except that when deleting the main
> instance - after the thread has been stopped -
> the __del__ method will not be carried out.
Does your deeply nested object hierarchy contain references back
onto the top level class instance itself? For example, run the following:
print "start"
class A:
def __init__(self):
print "init"
def __del__(self):
print "del"
a = A()
del a
class B:
def __init__(self):
print "init"
self.a = self
def __del__(self):
print "del"
b = B()
del b
print "finish"
And you get:
start
init
del
init
finish
Ie., you don't see "del" for class B. This is because B had a
member variable which looped back onto the object itself.
Same thing can happen where a class holds a reference back to
a method of the class, ie., a callback.
I don't believe that Python can cope with this and the object
will never actually be deleted. Please correct me if I am wrong.
In certain circumstances where I have had this problem and I
needed to know the object would be destroyed as soon as
possible, I have had to add a method to the class which could
be called to undo any references the class held which pointed
back onto itself.
I have looked at weak references, but have not been satisfied
that it could be used in the situation where I had to overcome the
problem.
More information about the Python-list
mailing list