python - Returning individual string representation of all objects in list of objects -


is possible return string representation (using __str__) of objects in list of objects different classes own __str__ function?

say have class contains methods being performed on bar objects. call class foo. then:

class foo:     def __init__(self):         self.bars = []  # contains bar objects      def __str__(self):         return "this foo object"  class bar:     def __init__(self, arg1):         self.arg1 = arg1      def __str__(self):         return "this bar object: " + arg1  def main():     foo = foo()     print(str(foo)) 

i want print string representation of of objects in self.bars when call main(), since creating instance of bar object not give me access string representation of of objects in self.bars.

to clarify, not asking same question in this post relating need of __repr__. instead, want return each object's string representation individually, loop.

is there anyway this, reasonably or unreasonably?

you said how this, use loop.

return "".join([str(x) + "\n" x in self.bars]) 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -