destructor not called
george.sakkis at gmail.com
Sun Sep 28 20:08:43 CEST 2008
On Sep 28, 12:00 pm, Marcin201 <marcin... at gmail.com> wrote:
> I have a class which uses a temporary directory for storing data. I
> would like that directory to be removed when the class is no longer
> used. I have tried removing the temporary directory from the class
> destructor, however, it was never called. After I while I traced the
> problem to the class having a reference to it's own function. Here is
> a simplified model.
> class Foo:
> def __init__(self):
> print "Hello"
> self.f = self.fxn
> def __del__(self):
> print "Bye"
> def fxn(self):
> print "function"
> a = Foo()
> running python test.py I get
> Is this an expected behavior or a bug in python? If this is expected
> any suggestions for working around this. I would like to avoid having
> to call the destructor explicitly.
Others have already replied to your main question; in short you
shouldn't rely on __del__ being called. Regardless, is there a (good)
reason for having an instance reference to the method ? Without
further information, that seems like a code smell.
More information about the Python-list