The __del__() and close() methods

The most common use for __del__() is to ensure that files are closed.

Generally, class definitions that open files will have something like what's shown in the following code:

__del__ = close 

This will ensure that the __del__() method is also the close() method. When the object is no longer needed, the file will be closed, and any operating system resources can be released.

Anything more complex than this is better done with a context manager. See Chapter 6, Using Callables and Contexts, for more information on context managers.

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

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