python - Understanding inheritance in practice. Printing values of an instance -
i have class of agents working __str__
method. have class of family(agents)
. family
structured dictionary agent id key. after allocating agents families, iterate on my_families
: when print members.keys()
, correct keys. when print members.values()
, list of instances
but, cannot access values inside instances. when try use method get_money()
answer family class not have method. help?
for family in my_families: print family.members.values()
thanks
providing more information. family class
class family(agents.agent): """ class set of agents """ def __init__(self, family_id): self.family_id = family_id # _members stores agent set members in dictionary keyed id self.members = {} def add_agent(self, agent): "adds new agent set." self.members[agent.get_id()] = agent
class of agents
class agent(): "class agent objects." def __init__(self, id, qualification, money): # self._id unique id number used 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 families def allocate_to_family(agents, families): dummy_agent_index = len(agents) family in families: dummy_family_size = random.randrange(1, 5) store = dummy_family_size while dummy_family_size > 0 , dummy_agent_index >= 0 , (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 printing example gets me instance objects not values
for family in my_families: print family.members.values()
if family
supposed contain agent
objects, why inherit agent
? regardless, never initialized parent agent
object in family
class's __init__
, , according edit get_money
method contained in agent
class, family
objects don't have get_money
method. access that, need first access family
object's members
dictionary, use key access desired agent
object, access object's get_money
method.
Comments
Post a Comment