I've learned that __str__
can define an output of the string of the object.
Example:
class Person(object):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
p1 = Person('Steve Jobs')
p2 = Person('Bill Gates')
p3 = Person('Mark Zuckerberg')
print(p1) # >>> Steve Jobs
it output Steve Jobs
as I wished, instead of <__main__.Person object at 0x10410c588>
However, if I create a list:
lst = [p1, p2, p3]
print(lst)
# >>> [<__main__.Person object at 0x1045433c8>, <__main__.Person object at 0x1045434e0>, <__main__.Person object at 0x104543550>]
I have to :
print([i.__str__() for i in lst])
# >>> ['Steve Jobs', 'Bill Gates', 'Mark Zuckerberg']
to make it work??
This does not make sense much, right?