I need to keep tracks of instances of some classes (and do other stuff with those classes). I would like to not have to declare any extra code in the classes in question, thus everything should ideally be handled in the metaclass.
What I can't figure out is how to add a weak reference to each new instance of those classes. For example:
class Parallelizable(type):
    def __new__(cls, name, bases, attr):
        meta = super().__new__(cls, name, bases, attr)
        # storing the instances in this WeakSet
        meta._instances = weakref.WeakSet()
        return meta
    @property
    def instances(cls):
        return [x for x in cls._instances]
class Foo(metaclass=Parallelizable)
    def __init__(self, name):
        super().__init__()
        self.name = name
        # I would like to avoid having to do that - instead have the metaclass manage it somehow
        self._instances.add(self)
Any ideas? I can't seem to find a hook on the metaclass side to get into the __init__ of Foo....