How do I get a list of all instances of a given class?
Python does not keep track of all instances of a class (or of a built-in type), so if you need this functionality, you have to implement it yourself. One way to do it is to store a weak reference to each instance in class attribute. Here’s an example:
import weakref class MyClass: _instances = set()