I want to get all the instances created for a class. Is there any way to get the instances without importing any module ?
I tried this way to append self in a class attribute.
class A(object):
instances = []
def __init__(self, foo):
self.foo = foo
A.instances.append(self)
foo = A("hi")
bar = A("Hey")
star = A("Hero")
for instance in A.instances:
print("Ins is ",instance)
Output
<__main__.A object at 0x148b8b6e0780>,
<__main__.A object at 0x148b8b6e0780>,
<__main__.A object at 0x148b8b6e0780>
I expect it to print foo,bar,star. Is there any way to get it in class without any module?