I'm working on one of the MIT 6.00 OCW problem sets, and I have the following code...
class Trigger(object):
def evaluate(self, story):
"""
Returns True if an alert should be generated
for the given news item, or False otherwise.
"""
raise NotImplementedError
# Whole Word Triggers
# Problems 2-5
# WordTrigger
class WordTrigger(Trigger):
def __init__(self, word):
self.word = word
def is_word_in(self, text):
word = self.word.lower()
text = text.lower()
#Remove punc and split text
for ch in string.punctuation:
text = text.replace(ch, ' ')
splitText = text.split(' ')
#Check if word is in text
return word in splitText
# TitleTrigger
class TitleTrigger(WordTrigger):
## def __init__(self, word):
## self.word = word
def evaluate(self, story):
return self.is_word_in(story.get_title())
I originally didn't comment out the constructor in TitleTrigger() but when I ran the provided test script it threw an error when the test utility tried to call my TitleTrigger() class. I couldn't figure out what was wrong so I peaked at the solution code and they had commented out the constructor, so that's what I did and it worked fine. This left me with three questions...
TitleTrigger() is an exact duplicate of the constructor in its super-class WordTrigger(), so why would it matter whether I defined it in the subclass or allowed the subclass to inherit it?WordTrigger() which are identical to TitleTrigger() except one looks for a word in the subject of a story and the other looks in summary. These two pass the tests just fine with their own constructors, which seems really bizarre.I can include the testing code if needed, though if the issue is just a quirk in how that code tests my classes I don't really need to know what that quirk is. What I'd really like to know for sure for future cases is whether subclasses automatically inherit their super-classes constructors if they (the subclasses) don't define their own, kind of like how they inherit other methods from the super-class.
Am I correct in thinking that subclasses will automatically inherit the constructor of their super-class if it's not defined in the subclass?
Yes. The concern in Java that prevents this from happening is overloading (i.e. everyone inherits from Object, so everyone would end up with a nullary constructor). Not an issue in Python, so we get to inherit constructors.
The constructor I used in TitleTrigger() is an exact duplicate of the constructor in its super-class WordTrigger(), so why would it matter whether I defined it in the subclass or allowed the subclass to inherit it?
It shouldn't (except for the sake of reducing code duplication), unless I'm missing something.
These two pass the tests just fine with their own constructors, which seems really bizarre.
Yes it is. I can't guess what's wrong without seeing more code/errors. Also, it seems like overkill to use classes here -- a function would do just fine. Not Java, so we don't need to wrap everything in useless layers of OO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With