Remember
With multiple inheritance, Python looks up methods using the class’s method resolution order. The declared base order matters.
With multiple inheritance, Python looks up methods using the class’s method resolution order. The declared base order matters.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
class Left:
def label(self):
return "left"
class Right:
def label(self):
return "right"
class Choice(Left, Right):
pass
print(Choice().label())Top-level scope
class LeftObject 4 · typeclass RightObject 5 · typeclass ChoiceWith multiple inheritance, Python looks up methods using the class’s method resolution order. The declared base order matters.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
class Left:
def label(self):
return "left"
class Right:
def label(self):
return "right"
class Choice(Left, Right):
pass
print(Choice().label())
|
v
DISPLAYED RESULT
left
FINAL BINDINGS
Left --> object 2 (type)
Right --> object 4 (type)
Choice --> object 5 (type)
object 2: class Left
object 4: class Right
object 5: class ChoiceChoose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
class Left:
def label(self):
return "left"
class Right:
def label(self):
return "right"
class Choice(Left, Right):
pass
print(Choice().label())Top-level scope
class LeftObject 4 · typeclass RightObject 5 · typeclass ChoiceThe example has finished. Compare the final output with the values and references above.
left
What do you expect this program to print?
class Left:
def label(self):
return "left"
class Right:
def label(self):
return "right"
class Choice(Left, Right):
pass
print(Choice().label())In Python, a class can inherit from more than one parent class. This is called Multiple Inheritance.
Don't worry if this sounds confusing. Let's break it down with very simple examples.
Inheritance means one class (called the child) can use code from another class (called the parent).
With multiple inheritance, a child class can use code from two or more parent classes.
# First Parent Class
class Father:
def skills(self):
print("Father: Cooking, Driving")
# Second Parent Class
class Mother:
def hobbies(self):
print("Mother: Painting, Gardening")
# Child class inherits from both Father and Mother
class Child(Father, Mother):
pass
# Create an object of Child
c = Child()
c.skills()
c.hobbies()
Father: Cooking, Driving
Mother: Painting, Gardening
Child class inherits from both Father and Mother.c can use functions from both parent classes.c.skills() comes from the Father class.c.hobbies() comes from the Mother class.This is where it gets interesting. Python follows something called MRO (Method Resolution Order).
Let's see an example:
class Father:
def talk(self):
print("Father: I talk about sports.")
class Mother:
def talk(self):
print("Mother: I talk about arts.")
class Child(Father, Mother):
pass
c = Child()
c.talk()
Father: I talk about sports.
Father and Mother have a method called talk().Child class inherits from Father first.Father and skips the one from Mother.You can check which class Python will look at first using this:
print(Child.__mro__)
(<class '__main__.Child'>, <class '__main__.Father'>, <class '__main__.Mother'>, <class 'object'>)
This tells you the order in which Python will check for methods:
Child.Father.Mother.object, which is the default base class in Python.Multiple inheritance lets a class use features from more than one parent. Just remember:
__mro__.Connect what you noticed in the example with the idea below.
With multiple inheritance, Python looks up methods using the class’s method resolution order. The declared base order matters.
Python chooses a parent method randomly.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.