Recall & Review
beginner
What is multiple inheritance in Python?
Multiple inheritance is a feature where a class can inherit attributes and methods from more than one parent class.Click to reveal answer
beginner
How do you define a class that inherits from two parent classes <code>Parent1</code> and <code>Parent2</code>?You write: <br><code>class Child(Parent1, Parent2):</code><br>This means <code>Child</code> inherits from both <code>Parent1</code> and <code>Parent2</code>.Click to reveal answer
intermediate
What happens if both parent classes have a method with the same name?
Python uses the Method Resolution Order (MRO) to decide which method to call. It looks for the method in the first parent class listed, then the next, and so on.Click to reveal answer
beginner
Write a simple example of multiple inheritance with two parent classes
A and B and a child class C.class A:
def greet(self):
return "Hello from A"
class B:
def greet(self):
return "Hello from B"
class C(A, B):
pass
c = C()
print(c.greet()) # Output: Hello from AClick to reveal answer
intermediate
Why should you be careful when using multiple inheritance?
Because it can make code harder to understand and debug, especially if parent classes have methods with the same name or complex relationships.
Click to reveal answer
How do you declare a class
Child that inherits from Parent1 and Parent2?✗ Incorrect
In Python, multiple inheritance is declared by listing parent classes separated by commas inside parentheses after the class name.
If both parents have a method named
show(), which parent's method is called in class Child(Parent1, Parent2)?✗ Incorrect
Python follows the Method Resolution Order (MRO) and calls the method from the first parent listed.
What does MRO stand for in Python multiple inheritance?
✗ Incorrect
MRO means Method Resolution Order, which is the order Python uses to look for methods in parent classes.
Which of these is a risk of using multiple inheritance?
✗ Incorrect
Multiple inheritance can make code complex and harder to maintain if not used carefully.
What keyword is used to define a class in Python?
✗ Incorrect
The keyword
class is used to define a class in Python.Explain how to write a Python class that inherits from two parent classes and how Python decides which parent method to use if both have the same method name.
Think about the order of parent classes and how Python searches for methods.
You got /4 concepts.
Describe one advantage and one disadvantage of using multiple inheritance in Python.
Consider how multiple inheritance helps and what problems it might cause.
You got /2 concepts.