3

I'm struggling with understanding a piece of code that's part of a larger problem set. The code is as follows (Note that WordTrigger is a subclass of Trigger):

class WordTrigger(Trigger):

    def __init__(self,word):
        self.word=word

    def isWordin(self, text):
        text = [a.strip(string.punctuation).lower() for a in text.split(" ")]
        for word in text:
            if self.word.lower() in word.split("'"):
                return True
            return False

So line 5 does the job of stripping the text of punctuation and making it lowercase. The string.split(" ") method creates a list of all the words in the text, splitting them and inserting blank spaces in between. The for-statement checks to see whether the 'word' is in the 'text'. So does it recognizes the variable 'word' from the constructor?

Does self.word.lower() make the word that was initialized by the constructor all lowercase? And does the 'if'-conditional in the 'for' loop make sure the search for 'alert' words doesn't exclude words with apostrophes?

2 Answers 2

3

So does it recognizes the variable 'word' from the constructor?

No. Variables defined within a method are local to that method, and object attributes (like self.word are not confused with local variables (like word).

Does self.word.lower() make the word that was initialized by the constructor all lowercase?

No. Strings are immutable in Python. It returns a new string -- a lower-cased version of self.word.

And does the 'if'-conditional in the 'for' loop make sure the search for 'alert' words doesn't exclude words with apostrophes?

Seems right to me.

4
  • Thank you! One follow-up question: Why is it necessary to make self.word lower case? Wasn't that already done in the line 'text = [a.strip(string.punctuation).lower() for a in text.split(" ")]'? In other words, doesn't that line make each 'a' (i.e. each word) in 'text' lower-case and strip it of its punctuation? Nov 18, 2012 at 16:03
  • @user1552159 No. Again, keep in the mind that word and self.word are different. The code would be less confusing if the variable names were changed a bit: for example, text_words rather than text, and then for tw in text_words.
    – FMc
    Nov 18, 2012 at 16:14
  • Ok, so then self.word refers to the object's attribute but 'word' (which I've changed to 'tw') refers to each element in a list of words (i.e. text_words) that was created by breaking up a chunk of text. So the reason that you need BOTH 'a.strip(string.punctuation).lower()' AND 'self.word.lower()' is that the first time .lower() is used, each element in the text_words list is being made lower case and the second time .lower() is used, the object's attribute 'word' (expressed by self.word) is being made lower case. Is that correct? Again, thank you for your help! Nov 18, 2012 at 16:42
  • @user1552159 Yes, that's a good explanation, and you're very welcome.
    – FMc
    Nov 18, 2012 at 16:49
2

1st Question: The for-statement checks to see whether the 'word' is in the 'text'. So does it recognizes the variable 'word' from the constructor?

The for statement's word is a local variable and is not the same as self.word. You can essentially replace that for loop with item or any variable name if you like.

2nd Question: Does self.word.lower() make the word that was initialized by the constructor all lowercase?

No it doesn't because they are two different things. the word local variable is each item in the list text. and self.word is the variable you pass in to the WordTrigger object when you first instantiate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.