How To Fix The AttributeError: ‘super’ object has no attribute When Call super().attribute_name in Subclass’s __init__() Function in Python

I create a parent class and a subclass using Python. The subclass inherit from the parent class. The parent class has a property color, and I initialize the color property in the parent class’s __init__(self, color) function. When I call super().color in the subclass’s __init__(self, color, size) function, it throws the “AttributeError: ‘super’ object has no attribute ‘color’” error.  I will tell you how to fix this error in this article.

1. The Reason for the AttributeError: ‘super’ object has no attribute ‘color’.

  1. There are several reasons that may trigger this error.
  2. The first reason is that attribute color is not defined in the parent class.
  3. The second reason is that the super() function does not directly allow you to access attributes ( such as color ) of the parent class in this manner.

2. How To Fix The AttributeError: ‘super’ object has no attribute ‘color’.

2.1 Check the Parent Class.

  1. Make sure that the `color` attribute is defined in the parent class.
  2. If it’s not, you need to define it in the parent class.
  3. Example of a Parent Class with a `color` attribute.
    class ParentClass:
        def __init__(self, color):
            self.color = color
    

2.2 Initialize the Parent Class.

  1. In the child class, make sure to call the constructor of the parent class using `super().__init__()` to properly initialize the attributes of the parent class.
  2. Example of a Child Class inheriting from the Parent Class.
    class ChildClass(ParentClass):
        def __init__(self, color, size):
            super().__init__(color)  # Call the parent class constructor
            self.size = size
    
        def display(self):
            print(f"Color: {self.color}, Size: {self.size}")
    

2.3 Create an Instance of the Child Class.

  1. When creating an instance of the child class, you should provide values for both the `color` and `size` attributes.
  2. Example of creating an instance of the Child Class.
    child_obj = ChildClass("Red", "Large")
    child_obj.display()  # This should not result in an 'AttributeError'
  3. By following these steps, you should be able to fix the “AttributeError: ‘super’ object has no attribute ‘color’” error and access the `color` attribute from the parent class using the `super()` function in your child class.

3. How To Fix The AttributeError: ‘super’ object has no attribute ‘color’ When Call super().color in Subclass’s __init__() Function.

  1. The section 2 shows you the correct steps to inherit and call a parent class’s attribute from a subclass.
  2. But when I add the code line super().color in the subclass’s __init__(self, color, size) function like below, it still throws the error out.
    class ParentClass:
        def __init__(self, color):
            self.color = color
    
    class ChildClass(ParentClass):
        def __init__(self, color, size):
            super().__init__(color)  # Call the parent class constructor
            print(super().color)
            self.size = size
    
        def display(self):
            print(f"Color: {self.color}, Size: {self.size}")
    
    if __name__ == "__main__":    
        child_obj = ChildClass("Red", "Large")
        child_obj.display()  # This should work without errors
    
  3. This is because the Python super() does not directly allow you to access attributes of the parent class in this manner.
  4. You should access the color attribute using self.color because you’ve already initialized it in the ChildClass constructor by calling super().__init__(color).
  5. Below is the correct Python code for the above example to avoid the AttributeError: ‘super’ object has no attribute ‘color’.
    class ParentClass:
        def __init__(self, color):
            self.color = color
    
    class ChildClass(ParentClass):
        def __init__(self, color, size):
            super().__init__(color)  # Call the parent class constructor
            #print(super().color)
            print(self.color)
            self.size = size
    
        def display(self):
            print(f"Color: {self.color}, Size: {self.size}")
    
    if __name__ == "__main__":    
        child_obj = ChildClass("Red", "Large")

4. What Does the super() Function Do in Python?

  1. The `super()` function does not return a class of an object; it returns a temporary object of the superclass (parent class) that allows you to call methods and access attributes of the superclass from the subclass.
  2. It is a way to access and invoke methods or attributes defined in a parent class when you are working in a subclass.
  3. The `super()` function is used to ensure that the method you are calling is from the parent class and not overridden in the current subclass.
  4. It’s often used in the context of method overriding to call the superclass’s implementation of a method.
  5. In Python, you typically use `super()` within a subclass to call the constructor of the parent class or to call methods defined in the parent class.
  6. It allows you to maintain a clear and organized hierarchy when working with inheritance.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.