__del__ doesn't work

Peter Abel PeterAbel at gmx.net
Sun Feb 15 14:41:07 EST 2004


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.

Tough a simple example works as expected:
>>> class A:
... 	def __init__(self):
... 		print 'Constructor of A()'
... 	def __del__(self):
... 		print 'Destructor of A()'
... 		
>>> a=A()
Constructor of A()
>>> del a
Destructor of A()

After all I know I can't expect that the __del__ method
is executed imediatly when deleting the instance
e.g. "del a" as in the example.

But the statements
import gc

and

gc.collect()

should assure that when the reference counter goes to zero
some time the __del__ method is executed.
But it never does.

So am I expecting something wrong
or am I doing something not in the right way?

The code is too complex to post it here or even a snippet.
But I think it's not a matter code but a matter of philosophy
that I may not have understood.
Any help would be appreciated.

Regards
Peter



More information about the Python-list mailing list