Suppose we have a class in python:
class A(object): def __del__(self): print "Del!"
__del__
causes the removal / garbage collection of any instance of A
Is it possible to do the same for a class? I would like to have some method called when the class itself is garbage collection, which I assume is executed in the output of the script.
Thanks in advance for any pointers!
Edit: As I expected, everyone is trying to drive me away from using this technique (I probably would have made such a comment myself :)), although the question still remains: is this possible?
I want the following: I have a class with a static member that needs to be cleared.
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()
called, but shortly before the program closes - i.e. when I know there will be no more instances of A
I know I can do this with atexit
, but it seems very ugly.
source share