Is there a way to get a list or return all the instances of a class within a class in python? I've already done some research into it and all the answers that I've seen assume the instances are global variables, not part of an existing class.
For example, can I find all instances of Bar that are instantiated within the class Foo below?
class Foo:
def __init__(self):
self.mymsg1 = Bar('John Doe')
self.mymsg2 = Bar('Jane Doe')
self.somenumber = 42
self.somewords = 'hello world'
class Bar:
def __init__(self, name):
self.hellomsg = 'hello ' + name
I want to be able to get mymsg1 and mymsg2 because they are Bar objects, but I don't want any of the other attributes or methods.