Alex Rivera | Logout

Method that gets called on module deletion in Python

Asked 2009-06-03T12:00:03.483
12

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?

Edit
Report

1 Answer

1

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.

answered 2009-06-03T12:05:17.153

Your Answer