Destructors are called when an object gets destroyed. In Python, destructors are not needed as much needed in C++ because Python has a garbage collector that handles memory management automatically.
The __del__() method is a known as a destructor method in Python. It is called when all references to the object have been deleted i.e when an object is garbage collected.
Syntax of destructor declaration :
def __del__(self): # body of destructor
Note : A reference to objects is also deleted when the object goes out of reference or when the program ends.
Example 1 : Here is the simple example of destructor. By using del keyword we deleted the all references of object ‘obj’, therefore destructor invoked automatically.
Employee created. Destructor called, Employee deleted.
Note : The destructor was called after the program ended or when all the references to object are deleted i.e when the reference count becomes zero, not when object went out of scope.
Example 2 :This example gives the explanation of above mentioned note. Here, notice that the destructor is called after the ‘Program End…’ printed.
Calling Create_obj() function... Making Object... Employee created function end... Program End... Destructor called
Example 3 : Now, consider the following example :
In this example when the function fun() is called, it creates an instance of class B which passes itself to class A, which then sets a reference to class B and resulting in a circular reference.
Generally, Python’s garbage collector which is used to detect these types of cyclic references would remove it but in this example the use of custom destructor marks this item as “uncollectable”.
Simply, it doesn’t know the order in which to destroy the objects, so it leaves them. Therefore, if your instances are involved in circular references they will live in memory for as long as the application run.
- Destructors in C#
- Perl | Constructors and Destructors
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- Important differences between Python 2.x and Python 3.x with examples
- MySQL-Connector-Python module in Python
- twitter-text-python (ttp) module - Python
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
- Use of min() and max() in Python
- set add() in python
- chr() in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.