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 5958423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:32:26+00:00 2026-05-22T18:32:26+00:00

Can someone explain why the following code behaves the way it does: import types

  • 0

Can someone explain why the following code behaves the way it does:

import types

class Dummy():
    def __init__(self, name):
        self.name = name
    def __del__(self):
        print "delete",self.name

d1 = Dummy("d1")
del d1
d1 = None
print "after d1"

d2 = Dummy("d2")
def func(self):
    print "func called"
d2.func = types.MethodType(func, d2)
d2.func()
del d2
d2 = None
print "after d2"

d3 = Dummy("d3")
def func(self):
    print "func called"
d3.func = types.MethodType(func, d3)
d3.func()
d3.func = None
del d3
d3 = None
print "after d3"

The output (note that the destructor for d2 is never called) is this (python 2.7)

delete d1
after d1
func called
after d2
func called
delete d3
after d3

Is there a way to “fix” the code so the destructor is called without deleting the method added? I mean, the best place to put the d2.func = None would be in the destructor!

Thanks

[edit] Based on the first few answers, I’d like to clarify that I’m not asking about the merits (or lack thereof) of using __del__. I tried to create the shortest function that would demonstrate what I consider to be non-intuitive behavior. I’m assuming a circular reference has been created, but I’m not sure why. If possible, I’d like to know how to avoid the circular reference….

  • 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-22T18:32:26+00:00Added an answer on May 22, 2026 at 6:32 pm

    I’m providing my own answer because, while I appreciate the advice to avoid __del__, my question was how to get it to work properly for the code sample provided.

    Short version: The following code uses weakref to avoid the circular reference. I thought I’d tried this before posting the question, but I guess I must have done something wrong.

    import types, weakref
    
    class Dummy():
        def __init__(self, name):
            self.name = name
        def __del__(self):
            print "delete",self.name
    
    d2 = Dummy("d2")
    def func(self):
        print "func called"
    d2.func = types.MethodType(func, weakref.ref(d2)) #This works
    #d2.func = func.__get__(weakref.ref(d2), Dummy) #This works too
    d2.func()
    del d2
    d2 = None
    print "after d2"
    

    Longer version:
    When I posted the question, I did search for similar questions. I know you can use with instead, and that the prevailing sentiment is that __del__ is BAD.

    Using with makes sense, but only in certain situations. Opening a file, reading it, and closing it is a good example where with is a perfectly good solution. You’ve gone a specific block of code where the object is needed, and you want to clean up the object and the end of the block.

    A database connection seems to be used often as an example that doesn’t work well using with, since you usually need to leave the section of code that creates the connection and have the connection closed in a more event-driven (rather than sequential) timeframe.

    If with is not the right solution, I see two alternatives:

    1. You make sure __del__ works (see this blog for a better
      description of weakref usage)
    2. You use the atexit module to run a callback when your program closes. See this topic for example.

    While I tried to provide simplified code, my real problem is more event-driven, so with is not an appropriate solution (with is fine for the simplified code). I also wanted to avoid atexit, as my program can be long-running, and I want to be able to perform the cleanup as soon as possible.

    So, in this specific case, I find it to be the best solution to use weakref and prevent circular references that would prevent __del__ from working.

    This may be an exception to the rule, but there are use-cases where using weakref and __del__ is the right implementation, IMHO.

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

Sidebar

Related Questions

Can someone please explain what the & does in the following: class TEST {
Can someone explain the following code sample what does album[photo_attributes][] mean I found this
Can someone explain what the following assembly code does? int 0x80
Can someone explain to me why the following code isn't outputting anything in a
Can someone explain the following code snippet for me? // Bind base object so
I'm having a problem with TRY...CATCH blocks. Can someone explain why the following code
Can someone explain the following piece of code int x = 45; int y
Can someone explain to me why the following code results in the error: According
Can someone explain to me why the following code compiles OK in Java? char
Given the following code, can someone explain why I can pass a COM object

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.