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 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

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 called when object s1 deleted using 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 above example, Destructor called, when the programs 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 some advantages of using Python destructor, That 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 destructor alon with examples.Destructor in Python is best for garbage collection because it the free the space in memory after the object deleted.

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

What is a destructor in Python?

Ans:- Desctructor in Python is speicla method that called automatically when the instance of the class get deleted.

How do you destroy an object in Python?

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

What is __del__ in Python?

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

What is __init__ in Python?

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

Happy Coding
~ Vishvajit Rao

For more reference:- Click Here

Python args and kwargs
Comprehension in Python

Was this post helpful?

Related Posts

Leave a Reply