destructor not called

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcin201

    destructor not called

    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
  • Szabolcs Ferenczi

    #2
    Re: destructor not called

    On Sep 28, 6:00 pm, Marcin201 <marcin...@gmai l.comwrote:
    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.
    The RAII (Resource Acquisition Is Initialization) pattern is not
    applicable to Python since the language concept is not suitable for
    it. The __del__ is not a genuine destructor. In your case it might not
    be performed when you expected it because there were still references
    left around to the object. You must take care to break those
    references.

    However, you can apply the EAM (Execute Around Method) pattern in
    Python to achieve the same effect. You can apply the EAM pattern with
    help of the `with' statement:

    with Foo() as a:
    # work with `a'

    In this case you must implement methods __enter__ and __exit__ instead
    of __init__ and __del__. The method __enter__ must return an instance
    of Foo.

    You can achieve the same effect with try-finally block as well:

    a = Foo()
    try:
    # work with `a'
    finally:
    # make `a' remove directories

    Best Regards,
    Szabolcs

    Comment

    • Roy Smith

      #3
      Re: destructor not called

      In article
      <2a7dea43-378a-423f-a5bb-d904b7f97869@p2 5g2000hsf.googl egroups.com>,
      Marcin201 <marcin201@gmai l.comwrote:
      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.
      The short answer is that destruction in Python is non-deterministic (a rude
      shock if you're used to C++). What you probably want is the new "with"
      statement (http://docs.python.org/ref/with.html).

      Comment

      • Michel Leunen

        #4
        Re: destructor not called

        Marcin201 a écrit :
        class Foo:
        def __init__(self):
        print "Hello"
        self.f = self.fxn
        Maybe self.f = self.fxn() is what you want. Note the '()'.

        --
        Michel Leunen

        Comment

        • George Sakkis

          #5
          Re: destructor not called

          On Sep 28, 12:00 pm, Marcin201 <marcin...@gmai l.comwrote:
          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

          Comment

          • Marcin201

            #6
            Re: destructor not called

            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.
            I have dictionary of fxns to do import/export based on the type of
            request from user so I can call self.Import['html'] or
            self.Import['text'].

            Thanks for everyones help.

            Marcin

            Comment

            Working...