Destructor

Destructors are called when an object gets destroyed. It’s the polar opposite of the constructor, which gets called on creation.

These methods are only called on creation and destruction of the object. They are not called manually but completely automatic.

Related course:
Python Beyond the Basics – Object-Oriented Programming

Example

The class below has a constructor (__init__) and destructor (__del__).
We create an instance from the class and delete it right after.

destructor

In code:

Run the program to see this output:

The output is displayed, even though we didn’t call any methods.

Previous Post

Leave a Reply