Python supports inheritance from multiple classes. In this lesson, you’ll see:
- How multiple inheritance works
- How to use
super()
to call methods inherited from multiple parents - What complexities derive from multiple inheritance
- How to write a mixin, which is a common use of multiple inheritance
A class can inherit from multiple parents. For example, you could build a class representing a 3D shape by inheriting from two 2D shapes:
class RightPyramid(Triangle, Square):
def __init__(self, base, slant_height):
self.base = base
self.slant_height = slant_height
def what_am_i(self):
return 'RightPyramid'
The Method Resolution Order (MRO) determines where Python looks for a method when there is a hierarchy of classes. Using super()
accesses the next class in the MRO:
Richard Morris on Jan. 14, 2020
Superbly motivated, organized, and paced. Most appreciated