How to keep track of class instances?

Question:

Toward the end of a program I’m looking to load a specific variable from all the instances of a class into a dictionary.

For example:

class Foo():
    def __init__(self):
        self.x = {}

foo1 = Foo()
foo2 = Foo()
...

Let’s say the number of instances will vary and I want the x dict from each instance of Foo() loaded into a new dict. How would I do that?

The examples I’ve seen in SO assume one already has the list of instances.

Asked By: DBWeinstein

||

Answers:

One way to keep track of instances is with a class variable:

class A(object):
    instances = []

    def __init__(self, foo):
        self.foo = foo
        A.instances.append(self)

At the end of the program, you can create your dict like this:

foo_vars = {id(instance): instance.foo for instance in A.instances}

There is only one list:

>>> a = A(1)
>>> b = A(2)
>>> A.instances
[<__main__.A object at 0x1004d44d0>, <__main__.A object at 0x1004d4510>]
>>> id(A.instances)
4299683456
>>> id(a.instances)
4299683456    
>>> id(b.instances)
4299683456    
Answered By: Joel Cornett

Using the answer from @Joel Cornett I’ve come up with the following, which seems to work. i.e. i’m able to total up object variables.

import os

os.system("clear")

class Foo():
    instances = []
    def __init__(self):
        Foo.instances.append(self)
        self.x = 5

class Bar():
    def __init__(self):
        pass

    def testy(self):
        self.foo1 = Foo()
        self.foo2 = Foo()
        self.foo3 = Foo()

foo = Foo()
print Foo.instances
bar = Bar()
bar.testy()
print Foo.instances

x_tot = 0
for inst in Foo.instances:
    x_tot += inst.x
    print x_tot

output:

[<__main__.Foo instance at 0x108e334d0>]
[<__main__.Foo instance at 0x108e334d0>, <__main__.Foo instance at 0x108e33560>, <__main__.Foo instance at 0x108e335a8>, <__main__.Foo instance at 0x108e335f0>]
5
10
15
20
Answered By: DBWeinstein

@JoelCornett’s answer covers the basics perfectly. This is a slightly more complicated version, which might help with a few subtle issues.

If you want to be able to access all the “live” instances of a given class, subclass the following (or include equivalent code in your own base class):

from weakref import WeakSet

class base(object):
    def __new__(cls, *args, **kwargs):
        instance = object.__new__(cls, *args, **kwargs)
        if "instances" not in cls.__dict__:
            cls.instances = WeakSet()
        cls.instances.add(instance)
        return instance

This addresses two possible issues with the simpler implementation that @JoelCornett presented:

  1. Each subclass of base will keep track of its own instances separately. You won’t get subclass instances in a parent class’s instance list, and one subclass will never stumble over instances of a sibling subclass. This might be undesirable, depending on your use case, but it’s probably easier to merge the sets back together than it is to split them apart.

  2. The instances set uses weak references to the class’s instances, so if you del or reassign all the other references to an instance elsewhere in your code, the bookkeeping code will not prevent it from being garbage collected. Again, this might not be desirable for some use cases, but it is easy enough to use regular sets (or lists) instead of a weakset if you really want every instance to last forever.

Some handy-dandy test output (with the instances sets always being passed to list only because they don’t print out nicely):

>>> b = base()
>>> list(base.instances)
[<__main__.base object at 0x00000000026067F0>]
>>> class foo(base):
...     pass
... 
>>> f = foo()
>>> list(foo.instances)
[<__main__.foo object at 0x0000000002606898>]
>>> list(base.instances)
[<__main__.base object at 0x00000000026067F0>]
>>> del f
>>> list(foo.instances)
[]
Answered By: Blckknght

Another option for quick low-level hacks and debugging is to filter the list of objects returned by gc.get_objects() and generate the dictionary on the fly that way. In CPython that function will return you a (generally huge) list of everything the garbage collector knows about, so it will definitely contain all of the instances of any particular user-defined class.

Note that this is digging a bit into the internals of the interpreter, so it may or may not work (or work well) with the likes of Jython, PyPy, IronPython, etc. I haven’t checked. It’s also likely to be really slow regardless. Use with caution/YMMV/etc.

However, I imagine that some people running into this question might eventually want to do this sort of thing as a one-off to figure out what’s going on with the runtime state of some slice of code that’s behaving strangely. This method has the benefit of not affecting the instances or their construction at all, which might be useful if the code in question is coming out of a third-party library or something.

Answered By: Walter Mundt

You would probably want to use weak references to your instances. Otherwise the class could likely end up keeping track of instances that were meant to have been deleted. A weakref.WeakSet will automatically remove any dead instances from its set.

One way to keep track of instances is with a class variable:

import weakref
class A(object):
    instances = weakref.WeakSet()

    def __init__(self, foo):
        self.foo = foo
        A.instances.add(self)

    @classmethod
    def get_instances(cls):
        return list(A.instances) #Returns list of all current instances

At the end of the program, you can create your dict like this:

foo_vars = {id(instance): instance.foo for instance in A.instances}
There is only one list:

>>> a = A(1)
>>> b = A(2)
>>> A.get_instances()
[<inst.A object at 0x100587290>, <inst.A object at 0x100587250>]
>>> id(A.instances)
4299861712
>>> id(a.instances)
4299861712
>>> id(b.instances)
4299861712
>>> a = A(3) #original a will be dereferenced and replaced with new instance
>>> A.get_instances()
[<inst.A object at 0x100587290>, <inst.A object at 0x1005872d0>]   
Answered By: OrenR61

You can also solve this problem using a metaclass:

  1. When a class is created (__init__ method of metaclass), add a new instance registry
  2. When a new instance of this class is created (__call__ method of metaclass), add it to the instance registry.

The advantage of this approach is that each class has a registry – even if no instance exists. In contrast, when overriding __new__ (as in Blckknght’s answer), the registry is added when the first instance is created.

class MetaInstanceRegistry(type):
    """Metaclass providing an instance registry"""

    def __init__(cls, name, bases, attrs):
        # Create class
        super(MetaInstanceRegistry, cls).__init__(name, bases, attrs)

        # Initialize fresh instance storage
        cls._instances = weakref.WeakSet()

    def __call__(cls, *args, **kwargs):
        # Create instance (calls __init__ and __new__ methods)
        inst = super(MetaInstanceRegistry, cls).__call__(*args, **kwargs)

        # Store weak reference to instance. WeakSet will automatically remove
        # references to objects that have been garbage collected
        cls._instances.add(inst)

        return inst

    def _get_instances(cls, recursive=False):
        """Get all instances of this class in the registry. If recursive=True
        search subclasses recursively"""
        instances = list(cls._instances)
        if recursive:
            for Child in cls.__subclasses__():
                instances += Child._get_instances(recursive=recursive)

        # Remove duplicates from multiple inheritance.
        return list(set(instances))

Usage: Create a registry and subclass it.

class Registry(object):
    __metaclass__ = MetaInstanceRegistry


class Base(Registry):
    def __init__(self, x):
        self.x = x


class A(Base):
    pass


class B(Base):
    pass


class C(B):
    pass


a = A(x=1)
a2 = A(2)
b = B(x=3)
c = C(4)

for cls in [Base, A, B, C]:
    print cls.__name__
    print cls._get_instances()
    print cls._get_instances(recursive=True)
    print

del c
print C._get_instances()

If using abstract base classes from the abc module, just subclass abc.ABCMeta to avoid metaclass conflicts:

from abc import ABCMeta, abstractmethod


class ABCMetaInstanceRegistry(MetaInstanceRegistry, ABCMeta):
    pass


class ABCRegistry(object):
    __metaclass__ = ABCMetaInstanceRegistry


class ABCBase(ABCRegistry):
    __metaclass__ = ABCMeta

    @abstractmethod
    def f(self):
        pass


class E(ABCBase):
    def __init__(self, x):
        self.x = x

    def f(self):
        return self.x

e = E(x=5)
print E._get_instances()
Answered By: sfinkens

Here’s a similar approach to Blckknght’s, which works with subclasses as well. Thought this might be of interest, if someone ends up here. One difference, if B is a subclass of A, and b is an instance of B, b will appear in both A.instances and B.instances. As stated by Blckknght, this depends on the use case.

from weakref import WeakSet


class RegisterInstancesMixin:
    instances = WeakSet()

    def __new__(cls, *args, **kargs):
        o = object.__new__(cls, *args, **kargs)
        cls._register_instance(o)
        return o

    @classmethod
    def print_instances(cls):
        for instance in cls.instances:
            print(instance)

    @classmethod
    def _register_instance(cls, instance):
        cls.instances.add(instance)
        for b in cls.__bases__:
            if issubclass(b, RegisterInstancesMixin):
                b._register_instance(instance)

    def __init_subclass__(cls):
        cls.instances = WeakSet()


class Animal(RegisterInstancesMixin):
    pass


class Mammal(Animal):
    pass


class Human(Mammal):
    pass


class Dog(Mammal):
    pass


alice = Human()
bob = Human()
cannelle = Dog()
Animal.print_instances()
Mammal.print_instances()
Human.print_instances()

Animal.print_instances() will print three objects, whereas Human.print_instances() will print two.

Answered By: ArthurPGB