I wrote a simple object oriented code with python. In first class named A I used __str__ so that I can print my objects. In another class I put those objects in obj_list. My question is, why I can print my object with class A, but when I want to print as print(my_object.obj_list) I do not get string representation of my object?
class A:
def __init__(self, name):
self.name = name
def __str__(self):
info = "My name is: " + self.name
return info
obj_1 = A("Mike")
obj_2 = A("Jon")
obj_3 = A("Steve")
print(obj_1, obj_2, obj_3)
class B:
def __init__(self):
self.obj_list = [obj_1, obj_2, obj_3]
my_object = B()
print(my_object.obj_list)
list.__str__only uses the__repr__method of any objects in the string. If you want a different string representation of a list, you need to build it yourself.