From 19b9f0acfb9235611701551a9055a225adb9ed91 Mon Sep 17 00:00:00 2001 From: Real Python Bot <42617967+realpython-bot@users.noreply.github.com> Date: Fri, 4 Dec 2026 14:38:14 +0000 Subject: [PATCH] Add python-super materials folder Co-Authored-By: Claude Opus 5.5 --- python-super/README.md | 26 ++++++++++++ python-super/cooperative.py | 61 ++++++++++++++++++++++++++++ python-super/mixin.py | 35 ++++++++++++++++ python-super/multiple_inheritance.py | 55 +++++++++++++++++++++++++ python-super/shapes.py | 28 +++++++++++++ python-super/single_inheritance.py | 34 ++++++++++++++++ python-super/super_deep_dive.py | 31 ++++++++++++++ 7 files changed, 270 insertions(+) create mode 100644 python-super/README.md create mode 100644 python-super/cooperative.py create mode 100644 python-super/mixin.py create mode 100644 python-super/multiple_inheritance.py create mode 100644 python-super/shapes.py create mode 100644 python-super/single_inheritance.py create mode 100644 python-super/super_deep_dive.py diff --git a/python-super/README.md b/python-super/README.md new file mode 100644 index 0000000000..4dca3e727f --- /dev/null +++ b/python-super/README.md @@ -0,0 +1,26 @@ +# Supercharge Your Classes With Python `super()` + +This folder provides the code examples for the Real Python tutorial [Supercharge Your Classes With Python `super()`](https://realpython.com/python-super/). + +## Setup + +The examples use only the Python standard library, so there's nothing to install. They were tested with Python 3.15 and run on any Python 3 release. + +## Usage + +Each script matches a section of the tutorial and prints the results you see in the tutorial's REPL sessions: + +| Script | Tutorial section | +|---|---| +| `shapes.py` | Rectangle and square without inheritance | +| `single_inheritance.py` | `super()` in Single Inheritance, What Can `super()` Do for You? | +| `super_deep_dive.py` | A `super()` Deep Dive | +| `multiple_inheritance.py` | Multiple Inheritance Overview, Method Resolution Order | +| `cooperative.py` | Cooperative Multiple Inheritance With `**kwargs` | +| `mixin.py` | Multiple Inheritance Alternatives | + +Run any script with Python: + +```console +$ python cooperative.py +``` diff --git a/python-super/cooperative.py b/python-super/cooperative.py new file mode 100644 index 0000000000..5570d955de --- /dev/null +++ b/python-super/cooperative.py @@ -0,0 +1,61 @@ +class Rectangle: + def __init__(self, length, width, **kwargs): + self.length = length + self.width = width + super().__init__(**kwargs) + + def area(self): + return self.length * self.width + + def perimeter(self): + return 2 * self.length + 2 * self.width + + +class Square(Rectangle): + def __init__(self, length, **kwargs): + super().__init__(length=length, width=length, **kwargs) + + +class Cube(Square): + def surface_area(self): + face_area = super().area() + return face_area * 6 + + def volume(self): + face_area = super().area() + return face_area * self.length + + +class Triangle: + def __init__(self, base, height, **kwargs): + self.base = base + self.height = height + super().__init__(**kwargs) + + def tri_area(self): + return 0.5 * self.base * self.height + + +class RightPyramid(Square, Triangle): + def __init__(self, base, slant_height, **kwargs): + self.base = base + self.slant_height = slant_height + kwargs["height"] = slant_height + kwargs["length"] = base + super().__init__(base=base, **kwargs) + + def area(self): + base_area = super().area() + perimeter = super().perimeter() + return 0.5 * perimeter * self.slant_height + base_area + + def area_2(self): + base_area = super().area() + triangle_area = super().tri_area() + return triangle_area * 4 + base_area + + +if __name__ == "__main__": + pyramid = RightPyramid(base=2, slant_height=4) + print(pyramid.area()) + print(pyramid.area_2()) diff --git a/python-super/mixin.py b/python-super/mixin.py new file mode 100644 index 0000000000..d391df51a4 --- /dev/null +++ b/python-super/mixin.py @@ -0,0 +1,35 @@ +class Rectangle: + def __init__(self, length, width): + self.length = length + self.width = width + + def area(self): + return self.length * self.width + + +class Square(Rectangle): + def __init__(self, length): + super().__init__(length, length) + + +class VolumeMixin: + def volume(self): + return self.area() * self.height + + +class Cube(VolumeMixin, Square): + def __init__(self, length): + super().__init__(length) + self.height = length + + def face_area(self): + return super().area() + + def surface_area(self): + return super().area() * 6 + + +if __name__ == "__main__": + cube = Cube(2) + print(cube.surface_area()) + print(cube.volume()) diff --git a/python-super/multiple_inheritance.py b/python-super/multiple_inheritance.py new file mode 100644 index 0000000000..564e844da6 --- /dev/null +++ b/python-super/multiple_inheritance.py @@ -0,0 +1,55 @@ +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 + + +class Square(Rectangle): + def __init__(self, length): + super().__init__(length, length) + + +class Triangle: + def __init__(self, base, height): + self.base = base + self.height = height + super().__init__() + + def tri_area(self): + return 0.5 * self.base * self.height + + +class RightPyramid(Square, Triangle): + def __init__(self, base, slant_height): + self.base = base + self.slant_height = slant_height + super().__init__(self.base) + + def area(self): + base_area = super().area() + perimeter = super().perimeter() + return 0.5 * perimeter * self.slant_height + base_area + + def area_2(self): + base_area = super().area() + triangle_area = super().tri_area() + return triangle_area * 4 + base_area + + +if __name__ == "__main__": + pyramid = RightPyramid(2, 4) + print(RightPyramid.__mro__) + print(pyramid.area()) + + # Rectangle.__init__() doesn't call super().__init__(), so + # Triangle.__init__() never runs and .height is never set: + try: + pyramid.area_2() + except AttributeError as error: + print("AttributeError:", error) diff --git a/python-super/shapes.py b/python-super/shapes.py new file mode 100644 index 0000000000..cdf62424de --- /dev/null +++ b/python-super/shapes.py @@ -0,0 +1,28 @@ +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 + + +class Square: + def __init__(self, length): + self.length = length + + def area(self): + return self.length * self.length + + def perimeter(self): + return 4 * self.length + + +if __name__ == "__main__": + square = Square(4) + print(square.area()) + rectangle = Rectangle(2, 4) + print(rectangle.area()) diff --git a/python-super/single_inheritance.py b/python-super/single_inheritance.py new file mode 100644 index 0000000000..1f1b90af68 --- /dev/null +++ b/python-super/single_inheritance.py @@ -0,0 +1,34 @@ +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 + + +class Square(Rectangle): + def __init__(self, length): + super().__init__(length, length) + + +class Cube(Square): + def surface_area(self): + face_area = super().area() + return face_area * 6 + + def volume(self): + face_area = super().area() + return face_area * self.length + + +if __name__ == "__main__": + square = Square(4) + print(square.area()) + + cube = Cube(3) + print(cube.surface_area()) + print(cube.volume()) diff --git a/python-super/super_deep_dive.py b/python-super/super_deep_dive.py new file mode 100644 index 0000000000..03283cf294 --- /dev/null +++ b/python-super/super_deep_dive.py @@ -0,0 +1,31 @@ +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 + + +class Square(Rectangle): + def __init__(self, length): + super(Square, self).__init__(length, length) + + +class Cube(Square): + def surface_area(self): + face_area = super(Square, self).area() + return face_area * 6 + + def volume(self): + face_area = super(Square, self).area() + return face_area * self.length + + +if __name__ == "__main__": + cube = Cube(3) + print(cube.surface_area()) + print(cube.volume())