Python Programming – Garbage Collection/Destroying Objects

Python facilitates the destruction of objects automatically when a certain object’s reference count reaches zero. That means Python deletes class instances automatically to free the memory space so that it can be utilized by some other program. The process of automatically and periodically cleaning the blocks of memory that is no longer required is called garbage collection. The garbage collector works automatically in the background to frees up any dereferenced space of memory. An object reference count increases when it is assigned a new name or place in a container and the object reference count decreases when it is deleted with the del statement as in the previous example Code 10.7.

As the garbage collector works automatically, we do not know in the front end when an object has been destroyed by the garbage collector. However, Python provides a special method _del_(), called a destructor, that is called when the instance is about to be destroyed. The syntax of destructor method _del_() is given as follows:

def del (self):

We see from the syntax that it contains self as the argument followed by a colon (:), i.e.,‘ function definition similar to the other class methods. The programming code to understand this concept is given in Code 10.10. From the output of the code, it is apparent that when we create an object the class method init () is invoked automatically and when we delete the instance of a class then the class method del () is invoked. In this program, we create three objects II, 12, and 13 of the class inventory. When the first object is created it calls the init () method to assign attributes to object II. We increment the value of class variable itemCount in the init () method and print the message that “No. of object created 1”. Similarly when the second call occurs when 12 objects are created the init( ) method increments and prints the message as “No. of objects created 2”. Likewise, in the case of the third object 13, the message “No. of object created 3” will be displayed.

Now we delete the reference of these three objects by using the del statement. On the occurrence of del II, the class method del () is invoked where the value of class variable itemCount is decremented by 1 and the message is printed as “No. of object destroyed 3”. Subsequently while deleting 12, and 13 objects the del () method is invoked and prints the messages as “No. of object destroyed 2” and “No. of object destroyed 1” for 12 and 13 respectively. In this way, all the objects are deleted by the Python garbage collector. Although the garbage collector of Python sweeps the memory automatically, by using the class method del () the procedure of destruction of objects can be understood effectively.

Code: 10.10. Illustration of destructor method__del__( )

#Illustration of destroying of objects using destructor method__de__ ( )

class Inventory:
‘An inventory class’
itemCount = 0

def init (self, item, code, price, quantity):                #initialization or
#constructor method
self.item item
self.code = code
self.price = price
self.quantity = quantity
Inventory.itemCount += 1
printCNo. of object created’, Inventory.itemCount)
def__del__(self): #destructor method of class Inventory
Inventory.itemCount -= 1 .
printCNo. of object destroyed’, Inventory.itemCount)

def displayltem(self): #displayStudent method of class Inventory
print (“\nItemName :”, self.item, “\nPrice:”, self.price, “\nQuantity:
“, self.quantity, “\ncode:”, self.code, “\nTotal:”, self.total)
11 =Inventory(‘Soap’, ‘SPl 11’, 30.5,4)
I2=Inventory(‘Shampoo’, ‘S0312′, 130.5,2)
I3=Inventory(’Oil’, ‘OL232’, 80.5, 1)
del II
del 12
del 13

Output

No. of object created 1
No. of object created 2
No. of object created 3
No. of object destroyed 2
No. of object destroyed 1
No. of object destroyed 0

Python Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *