Messing with the GC

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jan 19 17:24:37 CET 2013


On Sat, 19 Jan 2013 14:47:16 +0000, Jens Thoms Toerring wrote:

> Ok, the destrucor for the first instance of the X class is called only
> after printing out "After", so the GC didn't delete the object before.
> But then there are obviously no calls of the destructors of neither the
> second instance of the X class nor of the Y class instance. Shouldn't
> they be invoked before the program ends?

You should avoid __del__ destructors whenever not absolutely necessary.

__del__ may not be called for objects that still exist when Python exits.

If you have a cycle of objects, and *any* of them have a __del__ method, 
it may be impossible for Python to work out a safe order for them to be 
deleted. Consequently they will never be reclaimed by the garbage 
collector.

http://docs.python.org/2/reference/datamodel.html#special-method-names

http://docs.python.org/2/library/gc.html#gc.garbage



-- 
Steven



More information about the Python-list mailing list