[Python-3000] PEP: Eliminate __del__
greg.ewing at canterbury.ac.nz
Sun May 6 01:46:38 CEST 2007
Giovanni Bajo wrote:
> class Holder:
> def __init__(self):
> self.resource = ....
> self.__wr = weakref(self.resource, ....)
> So, are you
> saying that it's possible that the weakreference refcount goes to zero
> *before* Holder's refcount?
No, but depending on the order in which the dict contents
gets decrefed when Holder is deallocated, the __wr attribute
may get deallocated before the resource attribute. If that
happens, the callback is never called.
I have run the following code with Python 2.3, 2.4 and 2.5
and it does not print "Cleaning up":
from weakref import ref
print "Cleaning up"
self.resource = Resource()
self.weakref = ref(self.resource, cleanup)
h = Holder()
> Are you saying that the fact that it works for me in real-world code is
> just out of luck and might randomically break?
If this is really what you're doing, then yes, it will
randomly break. I may have misunderstood exactly what it
is you're doing, however.
More information about the Python-3000