Python OOPs


Python SQLite


Examples


Others


Destructor in Python


  • A Destructor is a function(method) which is used to deallocate memory of an object.
  • The destructor method is executed when an object is destroyed.
  • __del__() is the destructor method in Python.
Destructors are not required in Python as they are in C++, because Python has a garbage collector that takes care of memory management automatically.
Syntax
class A:
    def __del__(self):
        #body of the destructor

Here is an example of how to define a destructor in Python

Example
class Data:
    def __init__(self):
        print("Constructor Called.Object Created")
    
    def __del__(self):
        print("Destructor Called.Object Destroyed")
        
obj=Data()

Output

Constructor Called.Object Created
Destructor Called.Object Destroyed  

Constructors in Python