Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 29 Dec, 2020
Geek Week

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.


  • Whenever an object is created, the constructor of that particular class is called.
  • Constructor is a function whose name is the same as that of class name and it doesn’t have any return type. A constructor is used to initialize the variables of a class.
  • Whenever a constructor is called which means a new object is created, we just have to increment a counter that will keep track of the no. of objects that particular class has.

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()



 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :