I'm making a framework for an agent based model and I have a class called agents. Since there's going to be thousands of agents in the simulation, I want to use the __ slots__, which will replace the default __ dict__ and reduce memory consumption. I have my code structured so that it gets the agent's parameter from a data table and I want to assign the values stored in the table to an attribute with the table's header name.
If the following is the data table,
| agent_name | location | attr1 | attr2 | |--------------|------------|-------------|------------| | agent smith | NY | some value | some value | | Neo | NY | some value | some value | | Morpheus | Zion | some value | some value |
then if I create the 3 agents, I want all of them to have a .agent_name, .location, .attr1, and .attr2 attributes.
# illustration of what I want
header_of_table = ["agent_name", "location", "attr1", "attr2"]
class agent:
__slots__ = header_of_table
def __init__(self, values_in_row):
# what I ideally want, I know this syntax doesnt work, but this is to get the idea across
for slotted_attribute, value in zip(self.__slots__, values_in_row):
self.slotted_attribute = value
I know you can use the .eval method inside the for loop, but I don't find that clean and I feel like there has to be a better way. I'm wondering if there's a way to iterate over the slots and assign value to each attributes.