destructor not called

Marcin201 marcin201 at gmail.com
Sun Sep 28 12:00:34 EDT 2008


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.

Thanks,

Marcin



More information about the Python-list mailing list