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 6, Using Callables and Contexts. Creating a context is much more predictable than dealing with __del__() and the Python garbage collection algorithm.

If a Python object has a related operating system resource, the __del__() method is the 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 because a namespace is being removed. The documentation on the __del__() method describes the circumstances as precarious and provides this additional note on exception processing—exceptions that occur during their execution are ignored, and a warning is printed to sys.stderr instead. See the warning here: https://docs.python.org/3/reference/datamodel.html?highlight=__del__#object.__del__.

For these reasons, a context manager is often preferable to implementing __del__().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset