Reputation: 807
I am trying to access the accessor getSentence()
however I am getting the error that
AttributeError: 'Sentence' object has no attribute '_string'
I looked at many libraries and they said the right way to return it is with
self._
Here is my code:
class Sentence:
def __init__(self, string):
self.string = string
def getSentence(self):
return self._string
def getWords(self):
return self._aList
def getLength(self):
return len(self._string)
def getNumWords(self):
return len(self._string.split())
hippo = Sentence("hello world")
hippo.getSentence()
Upvotes: 0
Views: 94
Reputation: 1123410
In __init__
you assign to self.string
, not self._string
. Simply correct that to self._string
or rename the attribute you access in getSentence()
to self.string
.
That said, you don't really need an accessor here. If you leave the name as string
, you can just use hippo.string
and be done with it. Accessors are not really that useful if all they do is return the value of an attribute.
Upvotes: 4