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.


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


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


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

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