destructor not called
George Sakkis
george.sakkis at gmail.com
Sun Sep 28 14:08:43 EDT 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.
>
> test.py
> 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
> Hello
>
> 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.
George
More information about the Python-list
mailing list