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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:25:40+00:00 2026-06-11T17:25:40+00:00

I am building a Python wrapper for a swig interface for a C++ program.

  • 0

I am building a Python wrapper for a swig interface for a C++ program.

I am interested in knowing if there is a way for a Python object to “know” how it was deleted. The idea is simple, if the object was deleted by the garbage collector the wrapper would do nothing to the underlying C++ object, but if the user deleted the object on purpose (“del object) then the wrapper would detect that and un-reference the underlying object so that it also get deleted.

I tried googling about this already, but could not find anything. The only thing I found is that (apparently) __del__ is called by both the garbage collector and the del command, and it seems that there is no way to distinguish which one called it.

Hoping someone proves me wrong!

  • 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-11T17:25:41+00:00Added an answer on June 11, 2026 at 5:25 pm

    CPython uses reference counting to track object lifetimes. Thus, all deletions are done by the garbage collector.

    For example, the following code creates two references; the __del__ hook only is called once, after the second reference to the object is deleted:

    >>> class Foo(object):
    ...     def __del__(self):
    ...         print "Bye bye!"
    ... 
    >>> spam = Foo()
    >>> bar = spam
    >>> del spam
    >>> del bar
    Bye bye!
    

    Thus, there is no way to detect an individual del statement through the __del__ hook; it is always only called by the garbage collector.

    Note that certain python implementations (most notably cpython), do not clear objects with a __del__ hook that are part of a circular dependency. This does not apply to all implementations, but it bears remembering.

    In any case, the use of reference counting to implement object deletion and garbage collection is itself an implementation detail, and non-CPython implementations could (and do) handle object deletion differently altogether.

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

Sidebar

Related Questions

I'm building my first Python program :) However, I installed Python 3.2 instead of
I'm interested in building a python script that can give me stats on how
I am building a simple python CGI script to work with Twilio. Right now,
Building a simple Python game of Rock, Paper, Scissors for learning purposes. I've read
Is there any IDE (like VS) with drag and drop support for building python
I have been building a large python program for a while, and would like
I'm building a simple Python raytracer with pure Python (just for the heck of
I am building a python based web server ( Yes, python is a bad
I'm building a Python extension module on Windows. The extension module is written in
I'm building a Python application that utilizes a bunch of translated strings. The directory

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.