Home/Support/Support Forum/Help with Python classes
New and improved user forum site coming soon
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

Help with Python classes

0 votes
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=):
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 =

house = Environment(init_state)

vacuum = Agent(percept=house.getstate(),location='A')

while house.getstate() != :
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?
asked Nov 8 in Python by jacques.nel New to the Community (0 points)

Please log in or register to answer this question.

2 Answers

0 votes
I don't know what Digi product you are working with, but the product may not support the class function or it could be that the version of Python supported by the device uses a different call for Class.
answered Nov 9 by mvut Veteran of the Digi Community (15,515 points)
0 votes
Hi this is Palak
I also worked with Python. And I also faced these kind of problem while I was developing Snake game using Python. In my recent search I have seen this post(https://www.techgeekbuzz.com/blog/python-attributeerror-str-object-has-no-attribute-append-solution/) over the internet it can helps you.

Thanks
answered Nov 11 by palak231 New to the Community (1 point)
...