Skip to content Skip to sidebar Skip to footer

Python Class Inheritance: Attributeerror: '[subclass]' Object Has No Attribute 'xxx'

I have the following base class and subclass: class Event(object): def __init__(self, sr1=None, foobar=None): self.sr1 = sr1 self.foobar = foobar self.s

Solution 1:

Your subclass should be:

classTypeTwoEvent(Event):

    def__init__(self, level=None, *args, **kwargs):
        super(TypeTwoEvent, self).__init__(*args, **kwargs)
        self.sr1 = level
        self.state = STATE_EVENT_TWO

Because you override the __init__ method, so you need to call the parent method if you want the parent behavior to happen.

Remember, __init__ is not a special method dispite its strange name. It's just the method automatically called after the object is created. Otherwise it's an ordinary method, and ordinary inheritance rules apply.

super(ClassName, self).__init__(arguments, that, goes, to, parents)

is the syntax to call the parent version of the method.

For *args and **kwargs, it just ensures we catch all additional arguments passed to __init__ and pass it to the parent method, as you child method signature didn't do it and the parent need these arguments to work.

Solution 2:

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.

classTypeTwoEvent(Event):
    def__init__(self, level=None, **kwargs):
        # the super call to set the attributes in the parent classsuper(TypeTwoEvent, self).__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.

Solution 3:

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).

Solution 4:

You need to call the __init__ method of the base class from the __init__ method of the inherited class.

See here for how to do this.

Solution 5:

I have had problem with this also, but i've putted super().__init__() on the bottom of my derived class and that's why it doesn't work. Because i try to use attributes that is not initialized.

Post a Comment for "Python Class Inheritance: Attributeerror: '[subclass]' Object Has No Attribute 'xxx'"