Alex Rivera | Logout

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

Asked 2012-04-22T14:05:36.420
50

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?

Edit
Report

3 Answers

49

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.

answered 2012-04-22T14:18:11.177
4

You're overriding the constructor (__init__) of the parent class. To extend it, you need to explicitly call the constructor of the parent with a super() call.

class TypeTwoEvent(Event):
    def __init__(self, level=None, **kwargs):
        # the super call to set the attributes in the parent class
        super().__init__(**kwargs)
        # now, extend other attributes
        self.sr1 = level
        self.state = STATE_EVENT_TWO

Note that the super call is not always at the top of the __init__ method in your sub-class. Its location depends on your situation and logic.

answered 2012-04-22T14:11:05.450
2

When the instance is created, its __init__ method is called. In this case, that is TypeTwoEvent.__init__. Superclass methods will not be called automatically because that would be immensely confusing.

You should call Event.__init__(self, ...) from TypeTwoEvent.__init__ (or use super, but if you're not familiar with it, read up on it first so you know what you're doing).

answered 2012-04-22T14:11:00.590

Your Answer