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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:51:20+00:00 2026-05-21T05:51:20+00:00

Lets assume we have a class in python: class A(object): def __del__(self): print Del!

  • 0

Lets assume we have a class in python:

class A(object):
    def __del__(self):
        print "Del!"

__del__ is called upon deleting/garbage collection of any A instance.

Is is possible to do the same for a class? I would like to have some method called when the class itself is garbage collected, which I assume is being done at the script exit.

Thanks in advance for any pointers!


Edit: Just as I have expected, everyone is trying to drive me away from using this technique (I would probably make such a comment myself:)), though the question still stands: is it possible?

I want to the the following: I have a class with a static member that needs to be cleaned.

class A(object):
    class Meta(type):
        def __new__(mcs, name, bases, attrs):
            attrs['conn'] = sqlite3.connect( DB_FILE )
            return type.__new__(mcs, name, bases, attrs)

    __metaclass__ = Meta

I would like A.conn.close() to be called, but just before the program closes – i.e. when I know that no more instances of A will be ever created. I know I can do this with atexit, but this just seems very ugly.

  • 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-21T05:51:21+00:00Added an answer on May 21, 2026 at 5:51 am

    The problem is that classes contain circular references back to themselves – so when they are deleted they are not easily collected – thus the __del__ method odf the metaclass is not called.

    I could trigger it being called using Pypy’s Python implementation, but not using cpython – either 2.6 or 3.2. And even to trigger that, I had to manually invoke the garbage collector –
    The Python environment at program exit is known to be full of inconsitencies, and the chances of the __del__ method being called while enough internal information on the class would exist to allow a sae shut down would be very slim.

    Here is my Pypy session where I did trigger the call to the class’ __del__

     ~]$ pypy                                                                                       
    Python 2.5.2 (78826, Nov 29 2010, 00:18:05)                                                                       
    [PyPy 1.4.0] on linux2                                                                                            
    Type "help", "copyright", "credits" or "license" for more information.                                            
    And now for something completely different: ``sorry, I'll teach the cactus how                                    
    to swim later''                                                                                                   
    >>>> import gc
    >>>> class Meta(type):         
    ....    def __del__(cls):                          
    ....       print ("Arghh!!")                                              
    ....                                                                      
    >>>> class A(object):                                                                     
    ....   __metaclass__ = Meta                                                                                     
    ....                                                                                                                                                                                                                
    >>>> del A                                                                                                        
    >>>> gc.collect()                                                                                                 
    Arghh!!                                                                                                           
    0                                                                                                                 
    >>>>
    

    2022 – as of Python 3.11 a06, the __del__ method in the metaclass works in cPython, if one calls gc.collect() after deleting all references to the class, just like it happens with pypy on this example.

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

Sidebar

Related Questions

No related questions found

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.