I have this base class and subclass:
class Event:
def __init__(self, sr1=None, foobar=None):
self.sr1 = sr1
self.foobar = foobar
# Event class wrappers to provide syntatic sugar
class TypeTwoEvent(Event):
def __init__(self, level=None):
self.sr1 = level
Later on, when I try to check the foobar attribute of a TypeTwoEvent instance, I get an exception. For example, testing this at the REPL:
>>> event = TypeTwoEvent()
>>> event.foobar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'TypeTwoEvent' object has no attribute 'foobar'
I thought that the base class attributes would be inherited by the subclass and that creating an instance of a subclass would instantiate the base class (and thus invoke its constructor). Therefore, I expected the foobar attribute value to be defaulted to None.
Why do TypeTwoEvent instances not have a foobar attribute, even though Event instances do?
The subclass should be:
Because
__init__is overridden, the base class’__init__code will only run if it is explicitly requested.Despite its strange name,
__init__is not specially treated. It gets called automatically after the object is created; but otherwise it’s an ordinary method, and ordinary inheritance rules apply.is the syntax to call the parent version of the method. Using
*argsand**kwargsallows us to catch additional arguments passed to__init__and pass them to the parent method; this way, when aTypeTwoEventis created, a value can be specified for thefoobar, along with anything else specific to the base class.