I want to call a method of class A, from class B; but I get an error which I do ot understand.
Can anyone help me resolve my issue?
Here’s my code and error:
class Environment:
def init(self,state):
self.loc1_ = ‘A’
self.loc2_ = ‘B’
self.state_ = state
def setloc1_state(self,state): #state='D'
self.state_[0] = state
class Agent:
def init(self,percept:list,location:‘str’,action:‘str’=‘N’,goal:list=[‘C’,‘C’]):
self.percept_ = percept
self.cost_ = 0
self.location_ = location
self.action_ = action
self.goal_ = goal
def clean(self):
place = self.getlocation()
if place == 'A':
Environment.setloc1_state(self,'C')
if place == 'B':
Environment.setloc2_state(self,'C')
def main():
init_state = [‘D’,‘D’]
house = Environment(init_state)
vacuum = Agent(percept=house.getstate(),location='A')
while house.getstate() != ['C','C']:
vacuum.execute_action(house.getstate())
print(house.getstate())
main()
I get this error:
AttributeError: ‘Agent’ object has no attribute ‘state_’; related to this line of the clean() function:
“Environment.setloc1_state(self,‘C’)”
I read several tutorials which explains this concept ofcalling class members from another class, and those examples worked. So, I’m not sure why this code snippet does not distinguish between the “self” variabble linked to the 2 different classes.
Please educate me?