Python, unlike some other object-oriented languages such as Java, allows the developer complete freedom in when and how superclass finalizers are called during object finalization. However, the developer has responsibility for ensuring that objects are properly cleaned up, and that all superclass __del__ methods are called.

Classes with a __del__ method (a finalizer) typically hold some resource such as a file handle that needs to be cleaned up. If the __del__ method of a superclass is not called during object finalization, it is likely that resources may be leaked.

A call to the __del__ method of a superclass during object initialization may be unintentionally skipped:

Ensure that all superclass __del__ methods are properly called. Either each base class's finalize method should be explicitly called, or super() calls should be consistently used throughout the inheritance hierarchy.

In the following example, explicit calls to __del__ are used, but SportsCar erroneously calls Vehicle.__del__. This is fixed in FixedSportsCar by calling Car.__del__.

  • Python Reference: __del__.
  • Python Standard Library: super.
  • Python Glossary: Method resolution order.