Dynamically change __del__
__peter__ at web.de
Fri Apr 30 15:47:40 EDT 2010
Nikolaus Rath wrote:
> Apparently Python calls the class attribute __del__ rather than the
> instance's __del__ attribute. Is that a bug or a feature? Is there any
> way to implement the desired functionality without introducing an
> additional destroy_has_been_called attribute?
For newstyle classes __special__() methods are always looked up in the
class, never in the instance.
Your best bet is probably to manage the lifetime of the instance explicitly
with try...finally or a context manager:
>>> import contextlib
>>> class T(object):
... def close(self):
... print "Cleaning up."
>>> with contextlib.closing(T()) as t:
... print t
<__main__.T object at 0x7fc50c1e61d0>
Alternatively you can try and cook up something with weakref.ref and a
More information about the Python-list