Why don’t my subclass instances contain the attributes from the base class (causing an AttributeError when I try to use them)?

The subclass should be:

class TypeTwoEvent(Event):    
    def __init__(self, level=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.sr1 = level

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.

super().__init__(arguments, that, go, to, parents)

is the syntax to call the parent version of the method. Using *args and **kwargs allows us to catch additional arguments passed to __init__ and pass them to the parent method; this way, when a TypeTwoEvent is created, a value can be specified for the foobar, along with anything else specific to the base class.

Leave a Comment