
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do I get a list of all instances of a given class in Python?
The gc or weakref module is used to get a list of all instances of a given class. First, we will install the gc module using pip −
pip install gc
To use the gc module, use the import −
import gc
Get the instances of a class using the gc module
In this example, we have created a Demo class with four instances −
ob1 = Demo() ob2 = Demo() ob3 = Demo() ob4 = Demo()
We loop through the objects in memory −
for ob in gc.get_objects():
Example
Using the isinstance(), each object is checked for an instance of the class Demo. Let us see the complete example −
import gc # Create a Class class Demo: pass # Four objects ob1 = Demo() ob2 = Demo() ob3 = Demo() ob4 = Demo() # Display all instances of a given class for ob in gc.get_objects(): if isinstance(ob, Demo): print(ob)
Output
<__main__.Demo object at 0x000001E0A407FC10> <__main__.Demo object at 0x000001E0A407EBC0> <__main__.Demo object at 0x000001E0A407EBF0> <__main__.Demo object at 0x000001E0A407EC20>
Display the count of instances of a class using the gc module
Example
In this example, we will calculate and display the count of instances −
import gc # Create a Class class Demo(object): pass # Creating 4 objects ob1 = Demo() ob2 = Demo() ob3 = Demo() ob4 = Demo() # Calculating and displaying the count of instances res = sum(1 for k in gc.get_referrers(Demo) if k.__class__ is Demo) print("Count the instances = ",res)
Output
Count the instances = 4
Display the instances of a class using weakref module
The weakref module can also be used to get the instances of a class. First, we will install the weakref module using pip −
pip install weakref
To use the gc module, use the import −
import weakref
Example
Let us now see an example −
import weakref # Create a Demo() function class Demo: instanceArr = [] def __init__(self, name=None): self.__class__.instanceArr.append(weakref.proxy(self)) self.name = name # Create 3 objects ob1 = Demo('ob1') ob2 = Demo('ob2') ob3 = Demo('ob3') # Display the Instances for i in Demo.instanceArr: print(i.name)
Output
ob1 ob2 ob3
- Related Articles
- How do I get list of methods in a Python class?
- How do I list all files of a directory in Python?
- How do I enumerate functions of a Python class?
- How do we get size of a list in Python?
- How to get a list of all the test methods in a TestNG class?
- How do we get the size of a list in Python?
- How do I get length of list of lists in Java?
- How do we check if a class is a subclass of the given super class in Python?
- How can I get a list of locally installed Python modules?
- How do I create a multidimensional list in Python?
- How to get a list of all the keys from a Python dictionary?
- How to get a list of all the values from a Python dictionary?
- How do I list all the columns in a MySQL table?
- How do I declare a global variable in Python class?
- How do I add a class to a given element using Javascript?

Advertisements