
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
When are python classes and class attributes garbage collected?
In Python, a class is a collection of objects. It is the blueprint from which objects are being created. It is a logical entity that contains some attributes and methods. Following is an example of a Python class -
class Tutorialspoint: print("Welcome to Tutorialspoint.") obj1 = Tutorialspoint()
When are Python classes Garbage collected?
In Python, the objects are eligible for garbage collection when the reference count of the object is zero.
Similarly, the classes are garbage collected when no instances of the class have been created and it is no longer accessible within the program. This automatic memory management ensures efficient resource utilization and helps prevent memory leaks.
Example
In the following example, we have created MyClass, and obj1 and obj2 are two references to the class. When we have deleted the instance object, the references to the class become zero, and the objects are collected by the garbage collector -
import sys import gc class MyClass: def __init__(self, data): self.data = data def __del__(self): print("Object is being garbage collected") # Create two separate objects obj1 = MyClass([1, 2, 3]) obj2 = MyClass([4, 5, 6]) # Delete both objects del obj1 print("Deleted obj1") del obj2 print("Deleted obj2")
Following is the output of the above code -
Object is being garbage collected Deleted obj1 Object is being garbage collected Deleted obj1
What are Python class attributes?
In Python, the variables that are defined inside the class and outside the methods are known as class attributes.
Example
Here, we have created an Area() class, and the side1 and side2 are the class attributes -
class Area: side1 = 5 #class attribute 1 side2 = 6 #class atribute 2 def Rectangle(self): print("Area of the rectangle -",self.side1*self.side2) Obj1 = Area() Obj1.Rectangle()
Following is the output of the above code -
Area of the rectangle - 30
When are class attributes garbage collected?
The class attributes are not directly collected by the garbage collector unless the entire class itself is deleted. If the class has zero references, then the class, along with its attributes, is eligible for garbage collection.
Example
In the following example, we have defined a class Area(), and side1 and side2 are the class attributes. Before deleting the class attributes, we are able to access them using an object.
But when we have deleted the object, the references to the class Area() become zero, the Obj1 is collected by the garbage collector, and we tried to access it, raised a NameError -
import gc class Area: side1 = 5 #class attribute 1 side2 = 6 #class atribute 2 def Rectangle(self): print("Area of the rectangle -",self.side1*self.side2) Obj1 =Area() Obj1.Rectangle() print("Before deleting class :",Obj1.side1) del Obj1 print("After deleting class :",Obj1.side1)
Following is the output of the above code -
Area of the rectangle - 30 Before deleting class: 5 Traceback (most recent call last): File "/home/cg/root/682c9745587be/main.py", line 13, in <module> print("After deleting class :",Obj1.side1) ^^^^ NameError: name 'Obj1' is not defined