Destructor
Jeff Epler
jepler at unpythonic.net
Tue May 21 16:02:26 EDT 2002
The destructor (__del__ method) of an object is never invoked while the
object still has non-weak references. (AKA "the refcount is nonzero")
So merely having
del f
or
f = None
may not cause this necessary condition, as in
f = g = Thingy()
del f
(g still has a strong reference to the object that f referenced)
Furthermore, as I undersand the Language Reference, the destructor need
not be invoked as soon as the refcount drops to zero -- in fact,
destructors could be called in a different order than the refcounts
decrease, or be postponed forever. So if you have
class C:
def __init__(self, name): self.name = name
def __del__(self): print "dying", self.name
c = C("c")
d = C("d")
del c
print "blah"
del d
you might get
blah
dying d
dying c
or
dying c
blah
dying d
or
blah
but certainly not
dying d
blah
dying c
(Probably the CPython implementation you have only prints the 'dying
c/blah/dying d' order, however)
The 1.5-era language manual only gives cycles and program termination
as reasons __del__ is not called. I haven't checked a newer version of
the language manual..
Jeff
More information about the Python-list
mailing list