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’.
- There are several reasons that may trigger this error.
- The first reason is that attribute color is not defined in the parent class.
- 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.
- Make sure that the `color` attribute is defined in the parent class.
- If it’s not, you need to define it in the parent class.
- Example of a Parent Class with a `color` attribute.
class ParentClass: def __init__(self, color): self.color = color
2.2 Initialize the Parent Class.
- 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.
- 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.
- When creating an instance of the child class, you should provide values for both the `color` and `size` attributes.
- Example of creating an instance of the Child Class.
child_obj = ChildClass("Red", "Large") child_obj.display() # This should not result in an 'AttributeError'
- 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.
- The section 2 shows you the correct steps to inherit and call a parent class’s attribute from a subclass.
- 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
- This is because the Python super() does not directly allow you to access attributes of the parent class in this manner.
- You should access the color attribute using self.color because you’ve already initialized it in the ChildClass constructor by calling super().__init__(color).
- 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?
- 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.
- It is a way to access and invoke methods or attributes defined in a parent class when you are working in a subclass.
- 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.
- It’s often used in the context of method overriding to call the superclass’s implementation of a method.
- 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.
- It allows you to maintain a clear and organized hierarchy when working with inheritance.