Here is a clear, simple, and exam-oriented explanation of Destroying Objects in Python.
⭐ Destroying Objects in Python
In Python, destroying an object means removing it from memory when it is no longer needed.
This process is also known as object destruction or deallocation of memory.
Python handles object destruction automatically using:
✔ Reference Counting
✔ Garbage Collection
✔ Destructor Method (__del__())
—————————————–
⭐ 1. Automatic Object Destruction
Python uses reference counting as its primary technique.
Each object keeps track of how many references point to it.
Whenever references drop to zero, Python automatically destroys the object.
Example:
a = [1,2,3]
b = a
del a # reference removed
del b # all references removed → object destroyed
After this, the list object is deleted from memory.
—————————————–
⭐ 2. Garbage Collection
If objects are part of circular references, reference count will not drop to zero.
Example:
a = []
b = []
a.append(b)
b.append(a)
Here:
apoints tobbpoints toa
Reference count never becomes zero.
Python’s garbage collector finds such unreachable objects and removes them.
—————————————–
⭐ 3. Destructor: __del__() Method
Python provides a special method named __del__(), called destructor, which is executed just before the object is destroyed.
✔ Syntax
def __del__(self):
# cleanup code
✔ Example of Destructor
class Demo:
def __init__(self):
print("Object created")
def __del__(self):
print("Object destroyed")
obj = Demo()
del obj # forces destruction
print("End of program")
Output:
Object created
Object destroyed
End of program
⭐ Important Notes About __del__()
✔ Destructor is called when the object’s memory is about to be released
✔ It may run automatically at the end of the program
✔ Not guaranteed to run immediately after del (depends on garbage collector)
✔ Should not be used for important tasks like saving data — better use with or try-finally
—————————————–
⭐ 4. Using del Keyword to Delete Objects
del removes a reference to an object.
If no references remain → object destroyed.
Example:
class A:
def __del__(self):
print("Destructor called")
x = A()
y = x
del x # object still exists
del y # now destructor runs
—————————————–
⭐ 5. Destructor for Cleaning Resources
Destructors are useful for:
✔ Closing database connections
✔ Releasing network resources
✔ Closing files
✔ Freeing custom memory allocations
Example:
class FileHandler:
def __init__(self, filename):
self.file = open(filename, "r")
def __del__(self):
self.file.close()
print("File closed")
—————————————–
⭐ Exam-Ready Short Answer
Destroying objects in Python is done automatically when their reference count drops to zero or when the garbage collector removes unreachable objects. Python also provides a destructor method __del__() that is called just before an object is destroyed. The del keyword can delete references to objects, triggering destruction when no references remain.
⭐ Perfect 5–6 Line Exam Answer
Object destruction in Python occurs when an object is no longer referenced. Python uses reference counting and garbage collection to automatically free memory. The del keyword can remove references to objects, and Python invokes the __del__() destructor method before deallocating the object. This ensures cleanup of resources like files or network connections.
