Python, unlike statically typed languages such as Java, allows complete freedom when calling methods during object destruction.
However, standard object-oriented principles apply to Python classes using deep inheritance hierarchies.
Therefore the developer has responsibility for ensuring that objects are properly cleaned up when
there are multiple
__del__ methods that need to be called.
__del__ method of a superclass is not called during object destruction it is likely that
that resources may be leaked.
A call to the
__del__ method of a superclass during object destruction may be omitted:
- When a subclass calls the
__del__method of the wrong class.
- When a call to the
__del__method of one its base classes is omitted.
Either be careful to explicitly call the
__del__ of the correct base class, or
super() throughout the inheritance hierarchy.
Alternatively refactor one or more of the classes to use composition rather than inheritance.
In this example, explicit calls to
__del__ are used, but
SportsCar erroneously calls
Vehicle.__del__. This is fixed in
FixedSportsCar by calling