-1

I have a class of Agents with a working __str__ method. I have a class of Family(Agents). The Family is structured as a dictionary with the Agent ID as key. After allocating agents to families, I iterate over my_families: When I print members.keys(), I get the correct keys. When I print members.values(), I get a list of instances

But, I cannot access the values themselves inside the instances. When I try to use a method get_money() I get an answer that Family class does not have that method. Any help?

for family in my_families:
    print family.members.values()

Thanks

Providing more information. Family class

class Family(agents.Agent):
"""
Class for a set of agents
"""
    def __init__(self, family_ID):
        self.family_ID = family_ID
        # _members stores agent set members in a dictionary keyed by ID
        self.members = {}

    def add_agent(self, agent):
        "Adds a new agent to the set."
        self.members[agent.get_ID()] = agent

Class of agents

class Agent():
    "Class for agent objects."
    def __init__(self, ID, qualification, money):
        # self._ID is unique ID number used to track each person agent.
        self.ID = ID
        self.qualification = qualification
        self.money = money

    def get_ID(self):
        return self.ID

    def get_qual(self):
        return self.qualification

    def get_money(self):
        return self.money

    def __str__(self):
    return 'Agent Id: %d, Agent Balance: %d.2, Years of Study: %d ' (self.ID, self.money, self.qualification)

#Allocating agents to families

def allocate_to_family(agents, families):
    dummy_agent_index = len(agents)
    for family in families:
        dummy_family_size = random.randrange(1, 5)
        store = dummy_family_size
        while dummy_family_size > 0 and dummy_agent_index >= 0 and (dummy_agent_index - dummy_family_size) > 0:
            family.add_agent(agents[dummy_agent_index - dummy_family_size])
            dummy_family_size -= 1
        dummy_agent_index -= store

Finally the printing example that gets me instance objects but not their values

for family in my_families:
    print family.members.values()
B Furtado
  • 1,488
  • 3
  • 20
  • 34
  • Can you also post the Family class code? – SuperBiasedMan Jun 10 '15 at 15:28
  • You need to provide more information (i.e., a minimal working example of your class definition) or your question is likely to be put on hold. – Rick Jun 10 '15 at 15:30
  • Please provide more info, like said before a minimal working example, so we can reproduce your error and test how to fix it. – spalac24 Jun 10 '15 at 15:31
  • I don't see `get_money()` defined anywhere in your code. – TigerhawkT3 Jun 10 '15 at 16:59
  • I think it was only a problem of printing after all: `for family in my_families: print family.members.keys() for key in family.members.keys(): print family.members[key].get_ID(), family.members[key].get_money()` – B Furtado Jun 10 '15 at 17:28

1 Answers1

1

If Family is supposed to contain Agent objects, why does it inherit from Agent? Regardless, you never initialized the parent Agent object in the Family class's __init__, and according to your edit the get_money method is contained in the Agent class, so Family objects don't have a get_money method. To access that, you would need to first access a Family object's members dictionary, then use a key to access the desired Agent object, then access that object's get_money method.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • Thats it. I had not initialed again. And I had also not used the correct key iteration. Now it is ok. Thanks @TigerhawkT3 . I would vote up, but I lost reputation with this question :( Sorry. I'll be more generous when explaining my questions in the future. – B Furtado Jun 10 '15 at 17:39