Babeeshka Babeeshka - 1 month ago 17
Python Question

Python Classes, Instance Variables, and Accessor Methods

I am working on a problem set for a Python class. We are being introduced to Classes. I am trying to (A) create a class called Sentence with a single parameter, string, and to create an instance variable that stores the sentence as a string. (B) Then to assign accessor methods for the class: getSentence(return the sentence as a string), getWords(return the list of words in the sentence), getLength(return the number of characters in the sentence), and getNumWords(return the number of words in the sentence). Below is what I have attempted thus far:

line = "This Tuesday is Election Day"

class Sentence:
def __init__(self, text, string, words, length, num):
self.text = sentence
self.string = str(self.text)
self.words = self.text.split()
self.length = len(self.text)
self.num = len(self.words)

def getSentence(self):
return self.string

def getWords(self):
return self.words

def getLength(self):
return self.length

def getNumWords(self):
return self.num



line.getWords()


Thank you for your time.

Below is the updated code that works:

class Sentence:
def __init__(self, string):
self.astring = str(string)

def getSentence(self):
return self.astring

def getWords(self):
return (self.astring).split()

def getLength(self):
return len(self.astring)

def getNumWords(self):
return len(self.getWords())



string = Sentence("The Election is tomorrow")

print (string.getSentence())
print (string.getWords())
print (string.getLength())
print (string.getNumWords())

Answer

Think of programming as a whack-a-mole game. You have to keep pounding away at the bugs. You can write a series of tests even before you write the program that express how the class should behave. There are various frameworks for doing this including unittest and nose, but the simplest thing is a series of assert statements. All they do is raise an exception if an expression is not true.

So, lets buitd some tests

class Sentence:
    def __init__(self, text, string, words, length, num):
        self.text = sentence
        self.string = str(self.text)
        self.words = self.text.split()
        self.length = len(self.text)
        self.num = len(self.words)

    def getSentence(self):
        return self.string

    def getWords(self):
        return self.words

    def getLength(self):
        return self.length

    def getNumWords(self):
        return self.num

line = "This Tuesday is Election Day"
assert Sentence(line), "can initialize"
assert Sentence(line).getSentence() == line, "can return sentence"
assert Sentence(line).getWords() == ['This', 'Tuesday', 'is',
    'Election', 'Day'], "can return words"
# etc...

I run it and I get

Traceback (most recent call last):
  File "v.py", line 22, in <module>
    assert Sentence(line), "can initialize"
TypeError: __init__() missing 4 required positional arguments: 'string', 'words', 'length', and 'num'

Oops, those parameters are completely unneeded, so I remove them and run again and I get

Traceback (most recent call last):
  File "v.py", line 22, in <module>
    assert Sentence(line), "can initialize"
  File "v.py", line 3, in __init__
    self.text = sentence
NameError: name 'sentence' is not defined

Oops, the parameter name was not correct... Keep doing that until it all works.