Menu Close

Destructor in Python

In this article, we are going to learn all about Destructor in Python. Python destructor is a special method that is called automatically when the object of the class deleted.

Before going through this article, you have to basic knowledge of Python constructor. In this guide, we will go through the Python destructor along with some different examples.

Destructor in Python

Destructor in Python is a special or magic method that is called automatically when the object gets deleted. Python constructor is almost similar to the destructor in Python.

The only difference between constructor and destructor is, Constructor is called automatically when the object or instance of the class is created and Destructor is also called automatically when the object or instance gets destroyed.

Create Destructor in Python

To create the destructor in Python, you have to use __del__() special method.

Syntax of Destructor

Syntax of Destructor as follows.


def __del__(self):
	// Body of the destructor

Python Destructor Example

Here we will take some examples to understand destructor in Python, so that you can understand destructor very easily.

Example:


class Student:
	def __init__(self, name, age):
		self.name = name
		self.age = age

	def printDetail(self):
		print(f"My name is {self.name} and I am {self.age} years old.")

	def __del__(self):
		print(f"{self.name} student is deleted.")


s1 = Student("Vishvajit Rao", 22)
s1.printDetail()
del s1

Output


My name is Vishvajit Rao and I am 22 years old.
Vishvajit Rao student is deleted.

In the above example, Destructor is called when object s1 is deleted using the del keyword.

Note: A reference of objects is also deleted when the object goes out of reference or the program ends.

Example:


class Student:
	def __init__(self, name, age):
		self.name = name
		self.age = age

	def printDetail(self):
		print(f"My name is {self.name} and I am {self.age} years old.")

	def __del__(self):
		print(f"{self.name} student is deleted.")


s1 = Student("Ankit", 25)
s1.printDetail()

Output


My name is Ankit and I am 25 years old.
Ankit student is deleted.

In the above example, Destructor called, when the program ends.

Note:- Destructor in Python is also called automatically when the program ends or when all the objects are deleted.

Example:


class Student:
	def __init__(self, name, age):
		self.name = name
		self.age = age

	def printDetail(self):
		print(f"My name is {self.name} and I am {self.age} years old.")

	def __del__(self):
		print(f"{self.name} student is deleted.")


s1 = Student("Ankit", 25)
s1.printDetail()
print("Program ends")

Output


My name is Ankit and I am 25 years old.
Program ends
Ankit student is deleted.

Advantages of using destructor in Python

Here are some advantages of using Python destructor, Those are very important to you if you working with destructor in Python.

  • From using destructor in Python, unneeded objects get deleted automatically, This free the memory space which is known as garbage collection.
  • You don’t need to call the destructor manually. It is a special method that is called automatically when the objects get deleted.

Conclusion

In this article, you have learned all about Python destructors along with examples. Destructor in Python is best for garbage collection because it free the space in memory after the object is deleted.

If you like this article, please share and keep visiting for python tutorials.


What is a destructor in Python?

Ans:- Destructor in Python is a special method that is called automatically when the instance of the class gets deleted.

How do you destroy an object in Python?

Ans:- To destroy an object in Python, use the del keyword.

What is __del__ in Python?

Ans:- __del__ method is a special method in Python that is called automatically when the object is destroyed.This method is also called Destructor.

What is __init__ in Python?

Ant:- __init__ in Python is a special method that is called automatically when the instance of an object of the class is created.

Happy Coding
~ Vishvajit Rao

For more reference:- Click Here

Previous Article
Next Article

Related Posts

Leave a Reply