Editor

Predict the super() Chain

Advanced · Predict the output · from Python Multiple Inheritance and MRO: How It Actually Works

From the lesson: Python Multiple Inheritance and MRO: How It Actually Works

Think about how you inherit traits from your parents. You might have your mom's eyes and your dad's sense of humor. You didn't pick one parent - you got traits from both.

Read the lesson

What does this code print? Trace through the super() calls following the MRO.

class Base:
    def show(self):
        print('Base')

class Left(Base):
    def show(self):
        print('Left')
        super().show()

class Right(Base):
    def show(self):
        print('Right')
        super().show()

class Child(Left, Right):
    def show(self):
        print('Child')
        super().show()

Child().show()
# What gets printed? Write your prediction:
print('Child')
print('Left')
print('Right')
print('Base')

More Multiple Inheritance exercises

All Python exercises · 50 practice problems · Object-Oriented Programming quiz