Open In App

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

Last Updated : 29 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • 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:

Python3




# 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

Previous Article
Next Article

Similar Reads

Python | Avoiding class data shared among the instances
Class attributes belong to the class itself and they will be shared by all the instances and hence contains same value of each instance. Such attributes are defined in the class body parts usually at the top, for legibility. Suppose we have the following code snippet : C/C++ Code # Python code to demonstrate # the working of the sharing # of data v
2 min read
How to parse XML and count instances of a particular node attribute in Python?
In this article, we will see how to parse XML and count instances of a particular node attribute in Python. What is XML? Extensible Markup Language (XML) Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. It is a markup language like
3 min read
PYGLET – Getting font instances used in Formatted Document
In this article we will see how we can get all the font used in the formatted document in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or can
3 min read
Create Derived Class from Base Class Universally in Python
In object-oriented programming, the concept of inheritance allows us to create a new class, known as the derived class, based on an existing class, referred to as the base class. This facilitates code reusability and structuring. Python provides a versatile way to create derived classes from base classes universally, allowing for flexibility and sc
3 min read
Call a Class Method From another Class in Python
In object-oriented programming, classes play a pivotal role in organizing and structuring code. Python, being an object-oriented language, allows the creation of classes and their methods to facilitate code modularity and reusability. One common scenario in programming is the need to call a method from one class within another class. In this articl
3 min read
Python program to count number of vowels using sets in given string
Given a string, count the number of vowels present in the given string using Sets. You can use sets to count the number of vowels in a given string in Python. Sets are useful to efficiently check for counting the unique occurrences of vowels. Input : GeeksforGeeksOutput : No. of vowels : 5Explaination: The string GeeksforGeeks contains 5 vowels in
4 min read
Python Bin | Count total bits in a number
Given a positive number n, count total bit in it. Examples: Input : 13 Output : 4 Binary representation of 13 is 1101 Input : 183 Output : 8 Input : 4096 Output : 13 We have existing solution for this problem please refer Count total bits in a number link. Approach#1: We can solve this problem quickly in Python using bin() function. Convert number
3 min read
Python | Count number of items in a dictionary value that is a list
In Python, dictionary is a collection which is unordered, changeable and indexed. Dictionaries are written with curly brackets, and they have keys and values. It is used to hash a particular key. A dictionary has multiple key:value pairs. There can be multiple pairs where value corresponding to a key is a list. To check that the value is a list or
5 min read
Python | Program to count number of lists in a list of lists
Given a list of lists, write a Python program to count the number of lists contained within the list of lists. Examples: Input : [[1, 2, 3], [4, 5], [6, 7, 8, 9]] Output : 3 Input : [[1], ['Bob'], ['Delhi'], ['x', 'y']] Output : 4 Method #1 : Using len() C/C++ Code # Python3 program to Count number # of lists in a list of lists def countList(lst):
5 min read
Python | Ways to count number of substring in string
Given a string and a substring, write a Python program to find how many numbers of substrings are there in the string (including overlapping cases). Let's discuss a few methods below. Method #1: Using re.findall() Method C/C++ Code # Python code to demonstrate # to count total number # of substring in string import re # Initialising string s = 'aba
4 min read
three90RightbarBannerImg