How to destroy an object in Python?

PythonServer Side ProgrammingProgramming

A class implements the special method __del__(), called a destructor, that is invoked when the instance is about to be destroyed. This method might be used to clean up any non memory resources used by an instance.

Example

This __del__() destructor prints the class name of an instance that is about to be destroyed −

#!/usr/bin/python

class Point:
   def __init__( self, x=0, y=0):
      self.x = x       self.y = y    def __del__(self):
      class_name = self.__class__.__name__       print class_name, "destroyed"
 pt1 = Point()
pt2 = pt1 pt3 = pt1
print id(pt1), id(pt2), id(pt3) # prints the ids of the obejcts
del pt1
del pt2
del pt3

Output

When the above code is executed, it produces following result −

3083401324 3083401324 3083401324
Point destroyed
raja
Published on 16-Jan-2018 11:25:21
Advertisements