Python Practice › OOP Programs › Program 10
Multiple inheritance
Write a Python program that demonstrates multiple inheritance.
Output
photo taken | calling ['SmartPhone', 'Camera', 'Phone', 'object']
Approach
- A class inherits from two parents; the MRO decides lookup order.
Solution
class Camera: def photo(self): return "photo taken" class Phone: def call(self): return "calling" class SmartPhone(Camera, Phone): pass s = SmartPhone()print(s.photo(), "|", s.call())print([c.__name__ for c in SmartPhone.__mro__])Output
photo taken | calling ['SmartPhone', 'Camera', 'Phone', 'object']
Line by line
Line 13
The method resolution order shows Python searches SmartPhone, then Camera, then Phone.
Learn the concept
New to this topic? Read the lesson, then come back.