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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:40:56+00:00 2026-06-15T12:40:56+00:00

The python documentation for __del__ explains that an object’s __del__ method can be called

  • 0

The python documentation for __del__ explains that an object’s __del__ method can be called when its reference count is reduced to zero. Is it always called immediately (i.e. synchronously)? Or is it possible that the __del__ method will be called “at some point in the future”?

Furthermore, does the answer to this question apply to Python in general? Or is some latitude granted to the language implementation? (I use CPython.)

For this question, assume the following:

  • The objects in question inherit from object.
  • The objects in question have a custom __del__ implementation.
  • The objects’ custom __del__ implementation does NOT create new references to any objects.
  • The objects in question are NOT part of a reference cycle.
  • The interpreter is not in the process of shutting down.

From what I can tell, there seems to be a lot of confusion around this topic. In an effort to side-step some unnecessary discussion, let me give some examples of unacceptable answers:

  • “Don’t use __del__! It’s confusing!”
  • “Don’t use __del__! It’s not ‘pythonic’.”
  • “Don’t use __del__, use a context manager instead.”
  • Any answer that makes an assertion about what is or isn’t guaranteed without citing a credible source. (Please feel free to cite other SO answers, but don’t rely on them as your sole source. “Credible” in this instance means something from the documentation, or a reputable person, or maybe even the CPython source itself.)

To rephrase this question with an example, consider this code:

class C(object):
    def __init__(self, i):
        self.i = i

    def __del__(self):
        print "C.__del__:", self.i

print "Creating list"
l = [C(0), C(1), C(2)]

print "Popping item 1"
l.pop(1)

print "Clearing list"
l = []

print "Exiting..."

which produces the following output:

$ python test_del.py
Creating list
Popping item 1
C.__del__: 1
Clearing list
C.__del__: 2
C.__del__: 0
Exiting...

Notice that in this example, __del__ was called synchronously. Is that behavior guaranteed by the language? (Keep in mind the assumptions listed above.)

  • 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-06-15T12:40:57+00:00Added an answer on June 15, 2026 at 12:40 pm

    From The Python Language Reference (Python 2.7.3 edition), Chapter 3, Data model:

    Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether — it is a matter of implementation quality how garbage collection is implemented, as long as no objects are collected that are still reachable.

    CPython implementation detail: CPython currently uses a reference-counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references. See the documentation of the gc module for information on controlling the collection of cyclic garbage. Other implementations act differently and CPython may change. Do not depend on immediate finalization of objects when they become unreachable (ex: always close files).

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

Sidebar

Related Questions

The Python documentation for unittest implies that the assertRaises() method can be used as
(I am using python 2.7) The python documentation indicates that you can pass a
The python documentation implies that duplicate items can exist within a list, and this
The Python documentation says that the __init__ method of each class is responsible for
I am trying to do python documentation generation with Sphinx. The problem is that
The Python documentation specifies that is is legal to omit the parentheses if a
I was looking at the builtin object methods in the Python documentation , and
The Python documentation for except says: For an except clause with an expression, that
In the Python documentation it says: A thread can be flagged as a daemon
Given the Python documentation for Thread.run() : You may override this method in a

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.