The __del__() method
The __del__()
method has a rather obscure use case.
The intent is to give an object a chance to do any cleanup or finalization just before the object is removed from memory. This use case is handled much more cleanly by context manager objects and the with
statement. This is the subject of Chapter 5, Using Callables and Contexts. Creating a context is much more predictable than dealing with __del__()
and the Python garbage collection algorithm.
In the case where a Python object has a related OS resource, the __del__()
method is a last chance to cleanly disentangle the resource from the Python application. As examples, a Python object that conceals an open file, a mounted device, or perhaps a child subprocess might all benefit from having the resource released as part of __del__()
processing.
The __del__()
method is not invoked at any easy-to-predict time. It's not always invoked when the object is deleted by a del
statement, nor is it always invoked when an object is deleted...