multiple inheritance
A class inheriting from more than one parent class, searched left to right.
In more detail
A class such as class Duck(Swimmer, Flyer) inherits every method of both parents. When both define the same name, the parent listed first wins, because Python follows the method resolution order. Listing a class before one of its own subclasses makes the order impossible, and Python raises TypeError when the class is created.
Example
class Swimmer:
def move(self): return 'swim'
class Flyer:
def move(self): return 'fly'
class Duck(Swimmer, Flyer): pass
print(Duck().move()) # swimRelated terms
- inheritance — Declaring one class or type as a specialised version of another, so it receives the parent's members and can be used wherever the parent is expected.
- method resolution order — The order in which Python searches a class and its parents for a method or attribute.
- diamond problem — The question of which method runs when two parent classes share one ancestor.
Taught in
- Exception Classes Of Your Own — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Exceptions Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Inheritance, super() And Polymorphism — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Multiple Inheritance, The MRO And Diamonds — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Mock, Review And Exam Day — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep