I have a class that keeps track of its instances in a class variable, something like this:
class Foo: by_id = {} def __init__(self, id): sel
You can create a class list and then call append in the init method as follows:
class Planet: planets_list = [] def __init__(self, name): self.name = name self.planets_list.append(self)
Usage:
p1 = Planet("earth") p2 = Planet("uranus") for i in Planet.planets_list: print(i.name)