I am wanting to initiate multiple instances of a sub classes that each take their attributes from a specific instance of a class, rather than the general class.
For example if I have buildings and rooms, every room needs to belong to a specific instance of a building, taking its attributes and methods.
class Building:
def __init__(self, buildingName, location):
self.buildingName = buildingName
self.location = location
# lots more intersting stuff
def Evacuate_Building(self):
# do some stuff
pass
class Room(Building):
def __init__(self, roomName, roomType):
self.roomName = roomName
self.roomType = roomType
# lots more intersting stuff
def Directions(self, Current_Location):
'''do some stuff involving this.roomName and this.location which comes from the specific instance of the class'''
pass
So say I have three buildings: 'North', 'South' and 'West'.
Each building has its own rooms.
Looking at just the North Building it has the rooms 'N1', 'N2', 'N3', 'N4'.
In my code I would go through and initiate the three Buildings first, then I would initiate the Rooms, some how linking them back to its corresponding Building.
This allows me to use the Directions method, which utalises the attribute location from its parent class instance, which is unique to that Building, not a general value. It also needs to be be able to use the method from the parent class, Evacuate_Building.
I could get around this by passing the location data every time I initiate a room in the more standard setup using super(buildingName, location) or Building.__ init__(self, buildingName, location) however this means I have to write the location for every single Room and if I add or change something in the Building, I need to change every single room initialisation and the init code if additional attributes have been added.
I can also copy a the attributes out by passing the instance of the Building as a parameter to the init and then going this.location = building.location but this also has the same problems as above plus I do not get the methods this way.
I want some way of passing the specific instance of the Building, so that the room inherits its specific attributes and its methods.
Any feedback, suggestions, criticism, completely different ways of doing this is welcome! Thank you in advance!