Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6187667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:05:57+00:00 2026-05-24T02:05:57+00:00

I am curious about the details of __del__ in python, when and why it

  • 0

I am curious about the details of __del__ in python, when and why it should be used and what it shouldn’t be used for. I’ve learned the hard way that it is not really like what one would naively expected from a destructor, in that it is not the opposite of __new__ / __init__.

class Foo(object):

    def __init__(self):
        self.bar = None

    def open(self):
        if self.bar != 'open':
            print 'opening the bar'
            self.bar = 'open'

    def close(self):
        if self.bar != 'closed':
            print 'closing the bar'
            self.bar = 'close'

    def __del__(self):
        self.close()

if __name__ == '__main__':
    foo = Foo()
    foo.open()
    del foo
    import gc
    gc.collect()

I saw in the documentation that it is not guaranteed __del__() methods are called for objects that still exist when the interpreter exits.

  1. how can it be guaranteed that for any Foo instances existing when the interpreter exits, the bar is closed?
  2. in the code snippet above does the bar get closed on del foo or on gc.collect()… or neither? if you want finer control of those details (e.g. the bar should be closed when the object is unreferenced) what is the usual way to implement that?
  3. when __del__ is called is it guaranteed that __init__ has already been called? what about if the __init__ raised?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:05:57+00:00Added an answer on May 24, 2026 at 2:05 am

    The way to close resources are context managers, aka the with statement:

    class Foo(object):
    
      def __init__(self):
        self.bar = None
    
      def __enter__(self):
        if self.bar != 'open':
          print 'opening the bar'
          self.bar = 'open'
        return self # this is bound to the `as` part
    
      def close(self):
        if self.bar != 'closed':
          print 'closing the bar'
          self.bar = 'close'
    
      def __exit__(self, *err):
        self.close()
    
    if __name__ == '__main__':
      with Foo() as foo:
        print foo, foo.bar
    

    output:

    opening the bar
    <__main__.Foo object at 0x17079d0> open
    closing the bar
    

    2) Python’s objects get deleted when their reference count is 0. In your example the del foo removes the last reference so __del__ is called instantly. The GC has no part in this.

    class Foo(object):
    
        def __del__(self):
            print "deling", self
    
    if __name__ == '__main__':
        import gc
        gc.disable() # no gc
        f = Foo()
        print "before"
        del f # f gets deleted right away
        print "after"
    

    output:

    before
    deling <__main__.Foo object at 0xc49690>
    after
    

    The gc has nothing to do with deleting your and most other objects. It’s there to clean up when simple reference counting does not work, because of self-references or circular references:

    class Foo(object):
        def __init__(self, other=None):
            # make a circular reference
            self.link = other
            if other is not None:
                other.link = self
    
        def __del__(self):
            print "deling", self
    
    if __name__ == '__main__':
        import gc
        gc.disable()   
        f = Foo(Foo())
        print "before"
        del f # nothing gets deleted here
        print "after"
        gc.collect()
        print gc.garbage # The GC knows the two Foos are garbage, but won't delete
                         # them because they have a __del__ method
        print "after gc"
        # break up the cycle and delete the reference from gc.garbage
        del gc.garbage[0].link, gc.garbage[:]
        print "done"
    

    output:

    before
    after
    [<__main__.Foo object at 0x22ed8d0>, <__main__.Foo object at 0x22ed950>]
    after gc
    deling <__main__.Foo object at 0x22ed950>
    deling <__main__.Foo object at 0x22ed8d0>
    done
    

    3) Lets see:

    class Foo(object):
        def __init__(self):
    
            raise Exception
    
        def __del__(self):
            print "deling", self
    
    if __name__ == '__main__':
        f = Foo()
    

    gives:

    Traceback (most recent call last):
      File "asd.py", line 10, in <module>
        f = Foo()
      File "asd.py", line 4, in __init__
        raise Exception
    Exception
    deling <__main__.Foo object at 0xa3a910>
    

    Objects are created with __new__ then passed to __init__ as self. After a exception in __init__, the object will typically not have a name (ie the f = part isn’t run) so their ref count is 0. This means that the object is deleted normally and __del__ is called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know any more details about google's web-crawler (aka GoogleBot)? I was curious
I'm curious about people's approaches to using stored procedures in a database that is
I'm curious about OpenID. While I agree that the idea of unified credentials is
I'm quite curious about this. In a broad way, how does one go about
I was just curious about the correct way to go about accessing a property
Just curious about how to overload them. The opAssign operators are like addAssign(+=) and
Just curious about what would be the django way of achieving the following :
I am curious about the Custom To-Many Relation Accessors. It makes sense that the
I'm curious about whether there is any way to fake out Array.isArray() with a
Just curious about the encodings that system is using when handling string storing(if it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.