destructor in python by R4R Team

Destructors-
-Destructors are called when an object gets destroyed. In Python, destructors are not needed as much needed in C++ because Python has a garbage collector that handles memory management automatically.
-The __del__() method is a known as a destructor method in Python. It is called when all references to the object have been deleted i.e when an object is garbage collected.


program-

#create class
class A():
def __init__(self):
print("Object is created")
def __del__(self):
print("Object is deleted")

#create object and delete
a=A()
del(a)


output-

Object is created
Object is deleted


-In this program, we create a class A, then we create an object so that constructor is automatically call then we delete the object of the class by del(a) then destructor function __del__() is automatically call.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!