So i have this class in python
class room (object):
def __init__(self,number):
self.room_number=number
def get_room_num(self):
return self.room_number
And i have another class called House, basically when i create the house object, i also need to create room objects for the house, and the number of rooms will be chosen by the user.
I have difficulty with creating dynamic objects (since it is specified by the user, we would have 1 room or 10 room), i can't give each room object a name since i won't know how many there are before hand.
So i tried like this
class House (object):
def __init__(self,num_of_rooms):
self.room_holder=[]
for i in range(num_of_rooms):
self.room_holder.append(room(i))
def __iter__(self):
return iter(self.room_holder)
is there a better way to do it?, if so please help
the iter function is there so i can iterate over it, example
mansion=House(10)
for i in mansion:
print i.get_room_num()