Printing all instances of a class

Posted on

Question :

Printing all instances of a class

With a class in Python, how do I define a function to print every single instance of the class in a format defined in the function?

Answer #1:

I see two options in this case:

Garbage collector

import gc
for obj in gc.get_objects():
    if isinstance(obj, some_class):

This has the disadvantage of being very slow when you have a lot of objects, but works with types over which you have no control.

Use a mixin and weakrefs

from collections import defaultdict
import weakref

class KeepRefs(object):
    __refs__ = defaultdict(list)
    def __init__(self):

    def get_instances(cls):
        for inst_ref in cls.__refs__[cls]:
            inst = inst_ref()
            if inst is not None:
                yield inst

class X(KeepRefs):
    def __init__(self, name):
        super(X, self).__init__() = name

x = X("x")
y = X("y")
for r in X.get_instances():
del y
for r in X.get_instances():

In this case, all the references get stored as a weak reference in a list. If you create and delete a lot of instances frequently, you should clean up the list of weakrefs after iteration, otherwise there’s going to be a lot of cruft.

Another problem in this case is that you have to make sure to call the base class constructor. You could also override __new__, but only the __new__ method of the first base class is used on instantiation. This also works only on types that are under your control.

Edit: The method for printing all instances according to a specific format is left as an exercise, but it’s basically just a variation on the for-loops.

Answered By: Torsten Marek

Answer #2:

You’ll want to create a static list on your class, and add a weakref to each instance so the garbage collector can clean up your instances when they’re no longer needed.

import weakref

class A:
    instances = []
    def __init__(self, name=None):
        self.__class__.instances.append(weakref.proxy(self)) = name

a1 = A('a1')
a2 = A('a2')
a3 = A('a3')
a4 = A('a4')

for instance in A.instances:
Answered By: MirkoT

Answer #3:

You don’t need to import ANYTHING! Just use “self”. Here’s how you do this

class A:
    instances = []
    def __init__(self):

    def printInstances(cls):
        for instance in cls.instances:

It’s this simple. No modules or libraries imported

Answered By: ????? ?????

Answer #4:

Very nice and useful code, but it has a big problem: list is always bigger and it is never cleaned-up, to test it just add print(len(cls.__refs__[cls])) at the end of the get_instances method.

Here a fix for the get_instances method:

__refs__ = defaultdict(list)

def get_instances(cls):
    refs = []
    for ref in cls.__refs__[cls]:
        instance = ref()
        if instance is not None:
            yield instance
    # print(len(refs))
    cls.__refs__[cls] = refs

or alternatively it could be done using WeakSet:

from weakref import WeakSet

__refs__ = defaultdict(WeakSet)

def get_instances(cls):
    return cls.__refs__[cls]
Answered By: Fabio Caccamo

Answer #5:

Same as almost all other OO languages, keep all instances of the class in a collection of some kind.

You can try this kind of thing.

class MyClassFactory( object ):
    theWholeList= []
    def __call__( self, *args, **kw ):
         x= MyClass( *args, **kw )
         self.theWholeList.append( x )
         return x

Now you can do this.

object= MyClassFactory( args, ... )
print MyClassFactory.theWholeList
Answered By: S.Lott

Answer #6:

Python doesn’t have an equivalent to Smallktalk’s #allInstances as the architecture doesn’t have this type of central object table (although modern smalltalks don’t really work like that either).

As the other poster says, you have to explicitly manage a collection. His suggestion of a factory method that maintains a registry is a perfectly reasonable way to do it. You may wish to do something with weak references so you don’t have to explicitly keep track of object disposal.

Answer #7:

It’s not clear if you need to print all class instances at once or when they’re initialized, nor if you’re talking about a class you have control over vs a class in a 3rd party library.

In any case, I would solve this by writing a class factory using Python metaclass support. If you don’t have control over the class, manually update the __metaclass__ for the class or module you’re tracking.

See for more information.

Answered By: Daniel Naab

Answer #8:

In my project, I faced a similar problem and found a simple solution that may also work for you in listing and printing your class instances. The solution worked smoothly in Python version 3.7; gave partial errors in Python version 3.5.

I will copy-paste the relevant code blocks from my recent project.

instances = [] 

class WorkCalendar:
    def __init__(self, day, patient, worker): = day
        self.patient = patient
        self.worker= worker
    def __str__(self):
        return f'{} : {self.patient} : {self.worker}'

In Python the __str__ method in the end, determines how the object will be interpreted in its string form. I added the : in between the curly brackets, they are completely my preference for a “Pandas DataFrame” kind of reading. If you apply this small __str__ function, you will not be seeing some machine-readable object type descriptions- which makes no sense for human eyes. After adding this __str__ function you can append your objects to your list and print them as you wish.

appointment= WorkCalendar("01.10.2020", "Jane", "John")

For printing, your format in __str__ will work as default. But it is also possible to call all attributes separately:

for instance in instances:

For detailed reading, you may look at the source:

Answered By: alperen atik

Leave a Reply

Your email address will not be published. Required fields are marked *