Learn Python
>>> oop_fundamentals.multiple_multilevel_inheritance

Multiple & Multilevel Inheritance in Python

#concept

Multilevel inheritance is a chain — a class inherits from a class that itself inherits from another, several generations deep. Multiple inheritance is different — a single class inherits directly from more than one parent class at once, combining behavior from each. Python supports both, though multiple inheritance needs a clear resolution order for cases where parents define the same method.

#how it works
1

Multilevel inheritance forms a chain

class C(B): where B is itself class B(A): — C gets access to everything both ancestors define.

2

Multiple inheritance combines parents directly

class C(A, B): lets C inherit from both A and B at once.

3

Conflicts are resolved by MRO

If both parents define the same method name, the Method Resolution Order decides which wins — generally the first-listed parent.

4

__mro__ shows the exact search order

ClassName.__mro__ reveals precisely how Python will look up a method or attribute.

5

Composition is often the simpler alternative

Once relationships get complex, many codebases prefer one class containing another instead of deep multiple inheritance.

#example
class Flyer:
def move(self):
print("Flying")
 
class Swimmer:
def move(self):
print("Swimming")
 
class Duck(Flyer, Swimmer):
pass
 
d = Duck()
d.move()
print(Duck.__mro__)
Output
Flying
(<class '__main__.Duck'>, <class '__main__.Flyer'>, <class '__main__.Swimmer'>, <class 'object'>)
1class Duck(Flyer, Swimmer): inherits from both classes directly, without adding a class body of its own beyond pass.
2Both parents define move(), but Duck’s Method Resolution Order checks Flyer first, since it’s listed first, so d.move() prints "Flying".
3Duck.__mro__ shows the exact search order Python uses: Duck itself, then Flyer, then Swimmer, then the built-in base object class every class ultimately inherits from.
#use cases
Modeling something that genuinely combines two distinct capabilities, like a Duck that both flies and swims
Building a chain of increasingly specific classes, like Vehicle, then Car, then ElectricCar
Mixing in reusable, focused behavior from small 'mixin' classes designed to be combined with others
Diagnosing which method actually runs when two parent classes define the same name, using __mro__

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.