I'm having difficulty with formatting when an instance that is a list is printed. If I defined a class and created a class instance as the following:
class Building():
def __init__(self):
self.rooms = []
def add_room(self, rm_num: str, occupied: str, type: str, size: str):
self.rooms.append([rm_num, occupied, type, size])
>>>b = Building()
>>>b.add_room('302', 'Y', 'single', 'small')
>>>b.add_room('105', 'Y', 'single', 'large')
The goal right now is for the output when the instance is printed to look like the following: (each item in the list instance on a separate line**)
>>>print(b)
302, Y, single, small
105, Y, single, large
How could I go about formatting through def __repr__(self)
?
Thanks so much in advance!
See this tutorial for repr. Also, look into list comprehension in python.
def __repr__(self):
return "\n".join(', '.join(x) for x in self.rooms)