Is it possible to return the string representation (using __str__) of all objects in a list of objects from a different classes own __str__ function?
Say I have a class that contains all methods being performed on Bar objects. Call this class Foo. Then:
class Foo:
def __init__(self):
self.bars = [] # contains Bar objects
def __str__(self):
return "This is the Foo object"
class Bar:
def __init__(self, arg1):
self.arg1 = arg1
def __str__(self):
return "This is Bar object: " + arg1
def main():
foo = Foo()
print(str(foo))
I want to print the string representation of all of the objects in self.bars WHEN I call main(), since creating an instance of the Bar object would not give me access to the string representation of all of the objects in self.bars.
To clarify, I am not asking the same question as in this post relating to the need of __repr__. Instead, I want to return each object's string representation individually, as with a for loop.
Is there anyway to do this, reasonably or unreasonably?