
PYTHON — Revised Title Super and Inheritance Hierarchy in Python
The most profound technologies are those that disappear. They weave themselves into the fabric of everyday life until they are indistinguishable from it. — Mark Weiser
Insights in this article were refined using prompt engineering methods.

PYTHON — Loading Dataset in Python
# Super and Inheritance Hierarchy in Python
In Python, understanding the concept of super() and its use in the inheritance hierarchy is crucial. The super() function allows access to overridden parent methods in an inheritance chain. This lesson will demonstrate the use of super() in Python by building on the concept of method overriding and single inheritance.
Method Overriding and Inheritance
Let’s start with a simple example to illustrate method overriding in an inheritance hierarchy. Consider the following Python code:
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * self.length + 2 * self.width
def what_am_i(self):
return 'Rectangle'
class Square(Rectangle):
def __init__(self, length):
super().__init__(length, length)
def what_am_i(self):
return 'Square'
class Cube(Square):
def surface_area(self):
face_area = self.area()
return face_area * 6
def volume(self):
face_area = super().area()
return face_area * self.length
def what_am_i(self):
return 'Cube'
def family_tree(self):
return self.what_am_i() + ' child of ' + super(Cube, self).what_am_i()In the above code, we have three classes: Rectangle, Square, and Cube. The Square class inherits from the Rectangle class, and the Cube class inherits from the Square class. Each class contains methods specific to their shapes.
Accessing Overridden Parent Methods
The use of super() is demonstrated in the Cube class, specifically in the volume() method. By calling super().area(), we can access the area() method from the parent class (Square) in the inheritance chain.
def volume(self):
face_area = super().area()
return face_area * self.lengthUsing super() to Access Parent Methods
The super() function is also used to access the parent class methods directly. Consider the following example:
class Cube(Square):
# ... (previous methods)
def family_tree(self):
return self.what_am_i() + ' child of ' + super(Cube, self).what_am_i()In the family_tree() method, super(Cube, self).what_am_i() is used to access the what_am_i() method from the parent Square class.
Conclusion
In this tutorial, we’ve explored the use of super() in Python to access overridden parent methods within an inheritance hierarchy. By leveraging super(), we can efficiently navigate the inheritance chain and access methods from parent classes. Understanding super() is essential for effective inheritance hierarchy management in Python.
These examples illustrate the practical use of super() in single inheritance scenarios. For more advanced inheritance patterns, such as multiple inheritance, additional considerations and complexities may arise.
In summary, the super() function is a powerful tool for method resolution in Python's inheritance hierarchy. By utilizing super(), developers can streamline code organization and create robust class structures.

