Open In App

How to count number of instances of a class in Python?

Instances of a class mean the objects created for a particular class. A single class can have multiple objects of it. Here, we will find the count of the number of instances of a class in Python.

Approach:



Below is the implementation:




# code
class geeks:
    
    # this is used to print the number
    # of instances of a class
    counter = 0
  
    # constructor of geeks class
    def __init__(self):
        
        # increment
        geeks.counter += 1
  
  
# object or instance of geeks class
g1 = geeks()
g2 = geeks()
g3 = geeks()
print(geeks.counter)

Output:



3
Article Tags :