Published Aug. 29, 2023, 3:13 a.m.
Do you ever wonder how Python manages its memory and cleans up after we're done using objects? That's where destructors come into play! Think of them as the responsible cleaners of Python – they don't destroy objects, but they do a fantastic job of tidying up.
Destructors are like specialized methods that Python's Garbage Collector uses to do some cleanup tasks before an object says goodbye and leaves the memory. Imagine it as an object's last request before it goes away!
When it's time for an object to go, Python's Garbage Collector politely calls the destructor method (__del__
) to tidy up. Once the destructor finishes its cleaning, the Garbage Collector takes over and clears the object from memory.
import time
class Test:
def __init__(self):
print("Object Initialization...")
def __del__(self):
print("Fulfilling Last Wish and performing clean up activities...")
t1 = Test()
t1 = None # Remove the reference to the object
time.sleep(5)
print("End of application")
Output:
Object Initialization...
Fulfilling Last Wish and performing clean up activities...
End of application
Explanation: In this example, we create an object t1
of the Test
class. When we set t1
to None
, the reference to the object is removed. After a short pause, we see the destructor doing its cleanup tasks, indicating the object is being destroyed.
An object is ready for cleanup (Garbage Collection or GC) when it's no longer needed and no references are pointing to it. Once the reference count drops to zero, the object gets a green signal for GC.
import time
class Test:
def __init__(self):
print("Constructor Execution...")
def __del__(self):
print("Destructor Execution...")
t1 = Test()
t2 = t1
t3 = t2
del t1
time.sleep(5)
print("object not yet destroyed after deleting t1")
del t2
time.sleep(5)
print("object not yet destroyed even after deleting t2")
print("I am trying to delete the last reference variable...")
del t3
Output:
Constructor Execution...
object not yet destroyed after deleting t1
object not yet destroyed even after deleting t2
I am trying to delete the last reference variable...
Destructor Execution...
Explanation: Here, we demonstrate how an object remains intact until the last reference variable (t3
) is removed. Only when all references are gone does the object's destructor get activated.
import time
class Test:
def __init__(self):
print("Constructor Execution...")
def __del__(self):
print("Destructor Execution...")
list_of_objects = [Test(), Test(), Test()]
del list_of_objects
time.sleep(5)
print("End of application")
Output:
Constructor Execution...
Constructor Execution...
Constructor Execution...
Destructor Execution...
Destructor Execution...
Destructor Execution...
End of application
Explanation: In this final example, we create a list containing instances of the Test
class. When we delete the list, each object's destructor gets to work.
In Conclusion
Destructors are the cleanup crew of Python. They make sure that memory is freed up, and resources are released properly. By understanding how destructors work, you'll be well-equipped to write efficient and reliable Python code.