Is there a method that I can add to my module, which will get called when destructing the class?
We have a simple class which has only static member functions and needs to clean up the database connection when unloading the module.
Was hoping there would be a __del__
method either for modules or classes that don't have instances?
The __del__() method is a known as a destructor method. It is called when an object is garbage collected which happens after all references to the object have been deleted.
It is possible to define a method that will be called just before an object's final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.
__del__ is a destructor method which is called as soon as all references of the object are deleted i.e when an object is garbage collected. Example: Here is the simple example of destructor. By using del keyword we deleted the all references of object 'obj', therefore destructor invoked automatically.
A destructor method is called when all references to an object have been destroyed. In Python, the __del__() method is referred to as a destructor method. Destructors aren't as important in Python as they are in C++, because Python contains a garbage collector that takes care of memory management automatically.
When destructing which class? I though you said module?
Your module lives until the interpreter stops. you can add something to run at that time using the "atexit" module:
import atexit
atexit.register(myfunction)
EDIT: Based on your comments.
Since you don't want it as a destructor, my answer above is correct. Just def another function (or static method if you wish) and register it with the atexit:
def close_database():
proceed_to_close()
import atexit
atexit.register(close_database)
Now a quick note on your definition.
You said the class doesn't have any instances. So why make it a class? Why not define the functions in the module level instead? modules are first-class objects, cached and imported only once...
Example, instead of defining database.py
:
class DataBase(object):
@staticmethod
def execute_some_query(query):
code_here()
some_code()
@staticmethod
def close_database():
proceed_to_close()
import atexit ; atexit.register(DataBase.close_database)
and using:
from database import DataBase
DataBase.execute_some_query(query)
You could do this instead on database.py
:
def execute_some_query(query):
code_here()
some_code()
def close_database():
proceed_to_close()
import atexit ; atexit.register(close_database)
And use it like this:
import database
database.execute_some_query(query)
Or better yet: Use sqlalchemy and avoid all this trouble of creating your own database interface.
The class destructor method you're looking for is __del__
. There are some nuances to when it's called, and to how exceptions and subclassing should be handled in __del__
, so be sure to read the official docs.
A quick note on terminology, too: in python a module
is the file in which your code is located... a namespace, in essence. A single module can contain many classes, variables, and functions. The __del__
method is located on the class, not on the module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With