1

I am building a simulator and I have many classes in different Python files. Currently, I created a dictionary in one of my classes (Building class), and then I am calling it from other classes to update or use the information stored inside it. I am storing my agents in that dictionary. The Building class has some other variables and methods. Since I only need the dictionary inside it for most of my classes, I don't want to inherit or use it as a composite. This might be a good way but I am suspicious about its memory consumption and not sure to use it. So, I am asking is it a good way to circulate a dictionary to keep track of my agents? What is a good way to do this? An example code will be much appreciated.

My idea so far:

# in Building.py
class Building(object):
    # This class includes all of the facilities in the building. 
    # It also includes several dictionaries to keep track of all of the facilities and agents.
    residentsDic = {}

    def __init__(self, A, B):
        self.A = A
        self.B = B
# in ResidentArrival.py
import Building
class ResidentArrival(object):
    # This generates resident agents according to a timetable. 
    # I did't write the whole class
    def __init__(self, C, D):
        self.A = A
        self.B = B
 
    def addResident(self, resident):
        Building.residentsDic[resident].arrivalTime = time.now()
# in Resident.py
class Resident:
    # Resident class
    blaBla
RookieScientist
  • 314
  • 2
  • 12
  • 2
    Unless you have millions of objects, it's silly to worry about memory use. Also remember that storing a reference to an existing object costs nothing. If you create an object and store it in 9 different lists, there's still only one object, and 9 pointers. – Tim Roberts Feb 25 '21 at 06:25
  • @TimRoberts Thanks for your comment Tim. Any thoughts about my method or a better way to do it? – RookieScientist Feb 25 '21 at 07:07
  • Well, we're getting deeply into philosophy and personal preference here. Designing an object hierarchy is an art form. I don't like the fact that the ResidentArrival class has to know about the Building class. I can agree with a building having to know about residents, but not the other way around. And your design only allows for one building. Will you ever need more? – Tim Roberts Feb 25 '21 at 19:18
  • No I won’t need any more buildings – RookieScientist Feb 26 '21 at 07:06

0 Answers0