Syntax of destructor declaration :
def __del__(self):
# body of destructor
# Python program to illustrate destructor
class Student:
# Initializing
def __init__(self):
print('Student created.')
# Deleting (Calling destructor)
def __del__(self):
print('Destructor called, Student deleted.')
obj = Student()
del obj
Student created.
Destructor called, Student deleted.
Here, the destructor was not called when the object was no longer in scope, but rather when the program terminated or when all references to the object were removed, or when the reference count was zero.
# Python program to illustrate destructor
class Student:
# Initializing
def __init__(self):
print('Student created')
# Calling destructor
def __del__(self):
print("Destructor called")
def Create_obj():
print('Object creation')
obj = Student()
print('ending function')
return obj
print('Calling Create_obj() function...')
obj = Create_obj()
print('Program End...')
Calling Create_obj() function...
Object creation
Student created
ending function
Program End...
Destructor called
The destroyer occasionally fails to function properly. Let’s examine those scenarios.
A circular reference is a cyclic dependency in Python. Python’s garbage collector runs automatically when a program exits. It will try to free unused objects by removing them from the program’s memory. If you have a circular reference, the garbage collector will run infinitely until your program eventually crashes. For instance,
class A:
def __init__(self):
print("Object A Created")
def __del__(self):
print("Object A Destroyed")
class B:
def __init__(self):
print("Object B Created")
def __del__(self):
print("Object B Destroyed")
#creating two objects
a = A()
b = B()
#setting up circular reference
a.b = b
b.a = a
#deleting objects
del a
del b
Output:
Object A Created
Object B Created
__init__
methodDestructors are only called in object-oriented programming when an object is successfully created because, in the event of an exception in the constructor, the constructor itself destroys the object.
However, in Python, the method del is called in the event that an exception occurs while initializing the object in the init method.
The del method will therefore attempt to empty all the resources and variables even though the object was never initialized correctly, which could result in another exception. For instance,
class Example():
def __init__(self, x):
# for x = 0, raise exception
if x == 0:
raise Exception();
self.x = x;
def __del__(self):
print (self.x)
# creating an object
myObj = Example();
# to delete the object explicitly
del myObj
Output:
Exception ignored in: <function Example.__del__ at 0x7f0118581e50>
Traceback (most recent call last):
File "<string>", line 9, in __del__
AttributeError: 'Example' object has no attribute 'x'
Traceback (most recent call last):
File "<string>", line 12, in <module>
TypeError: __init__() missing 1 required positional argument: 'x'
Note: also read about Constructor in Python
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…