Here's the code made me confused:
class Fruit():
def __init__(self, name, price):
self.name = name
self.price = price
def __str__(self):
return "%s is %d" % (self.name,self.price)
L = [Fruit("Cherry", 10), Fruit("Apple", 5), Fruit("Blueberry", 20)]
print(Fruit("Cherry", 10))
print(L)
OUTPUT:
Cherry is 10
[<__main__.Fruit object>, <__main__.Fruit object>, <__main__.Fruit object>]
Since I've defined the str method, so I can print out an instance as "Cherry is 10", but when I print the list L, those instances in the list couldn't be printed out.
I'm wondering what's the logic behind it. Is it because the print() doesn't read the sub layer of the list?