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

  • SEARCH
  • Home
  • 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 7685049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:10:00+00:00 2026-05-31T19:10:00+00:00

I would like to understand how object deletion works on python. Here is a

  • 0

I would like to understand how object deletion works on python. Here is a very simple bunch of code.

class A(object):

    def __init__(self):
        setattr(self, "test", self._test)

    def _test(self):
        print "Hello, World!"

    def __del__(self):
        print "I'm dying!"

class B(object):

    def test(self):
        print "Hello, World!"

    def __del__(self):
        print "I'm dying"

print "----------Test on A"
A().test()
print "----------Test on B"
B().test()

Pythonista would recognize that I’m running a python 2.x version. More specially, this code runs on a python 2.7.1 setup.

This code outputs the following:

----------Test on A
Hello, World!
----------Test on B
Hello, World!
I'm dying

Surprisingly, A object is not deleted. I can understand why, since the setattr statement in __init__ produces a circular reference. But this one seems to be easy to resolve.

Finally, this page, in python documentation (Supporting Cyclic Garbage Collection), show that it’s possible to deal with this kind of circular reference.

I would like to know:

  • why I never go thru my __del__ method in A class?
  • if my diagnosis about circular reference is good, why my object subclass does not support cyclic garbage collection?
  • finally, how to deal with this kind of setattr if I really want to go thru __del__?

Note: In A if the setattr points to another method of my module, there’s no problem.

  • 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-31T19:10:01+00:00Added an answer on May 31, 2026 at 7:10 pm

    Fact 1

    Instance methods are normally stored on the class. The interpreter first looks them up in the instance __dict__, which fails, and then looks on the class, which succeeds.

    When you dynamically set the instance method of A in __init__, you create a reference to it in the instance dictionary. This reference is circular, so the refcount will never go to zero and the reference counter will not clean A up.

    >>> class A(object):
    ...     def _test(self): pass
    ...     def __init__(self):
    ...             self.test = self._test
    ... 
    >>> a = A()
    >>> a.__dict__['test'].im_self
    

    Fact 2

    The garbage collector is what Python uses to deal with circular references. Unfortunately, it can’t handle objects with __del__ methods, since in general it can’t determine a safe order to call them. Instead, it just puts all such objects in gc.garbage. You can then go look there to break cycles, so they can be freed. From the docs

    gc.garbage
    

    A list of objects which the collector found to be unreachable but could
    not be freed (uncollectable objects). By default, this list contains only
    objects with __del__() methods. Objects that have __del__() methods
    and are part of a reference cycle cause the entire reference cycle
    to be uncollectable, including objects not necessarily
    in the cycle but reachable only from it. Python doesn’t collect such
    cycles automatically because, in general, it isn’t possible for Python
    to guess a safe order in which to run the __del__() methods. If you
    know a safe order, you can force the issue by examining the garbage
    list, and explicitly breaking cycles due to your objects within the
    list. Note that these objects are kept alive even so by virtue of
    being in the garbage list, so they should be removed from garbage too.
    For example, after breaking cycles, do del gc.garbage[:] to empty the
    list. It’s generally better to avoid the issue by not creating cycles
    containing objects with __del__() methods, and garbage can be examined
    in that case to verify that no such cycles are being created.

    Therefore

    Don’t make cyclic references on objects with __del__ methods if you want them to be garbage collected.

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

Sidebar

Related Questions

I would like to understand better how Gridview works, in particular auto_fit . Here
I would like to understand that what is Mesh Object and its conection with
I would like to understand what class << self stands for in the next
I would like to understand relationship between jQuery object and DOM element.. When jQuery
I am trying to understand HTML5 history object. Here is a simple example which
I would like to wipe a NSString object in my code (replace every characters
I would like to understand why the following doesn't work: public class HelloClass {
Someone gave me this code that works great. But I would really like to
I am developing a Rails application and would like to understand when to use
I would like to better understand the basic steps needed to a take an

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.