Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is multiple inheritance in Python?
Multiple inheritance is when a class inherits from more than one parent class, gaining features from all of them.
Click to reveal answer
beginner
Why should you be careful when using multiple inheritance?
Because it can create complex relationships that are hard to understand and debug, especially if parent classes have methods with the same name.
Click to reveal answer
intermediate
What is the Method Resolution Order (MRO) in Python?
MRO is the order Python follows to look for methods in parent classes when a method is called on a child class. It helps avoid confusion in multiple inheritance.
Click to reveal answer
intermediate
Name one best practice to avoid problems with multiple inheritance.
Use super() to call parent methods, so Python follows the MRO and avoids calling the same method multiple times.
Click to reveal answer
intermediate
When is it better to avoid multiple inheritance?
When the class hierarchy becomes too complicated or when composition (using objects inside classes) can achieve the same goal more clearly.
Click to reveal answer
What does the Method Resolution Order (MRO) determine in Python multiple inheritance?
AThe order classes are defined in the file
BThe order Python looks for methods in parent classes
CThe order methods are written inside a class
DThe order Python imports modules
✗ Incorrect
MRO defines the order Python uses to find methods in parent classes when multiple inheritance is involved.
Which function helps safely call parent class methods in multiple inheritance?
Abase()
Bparent()
Csuper()
Dinherit()
✗ Incorrect
super() calls the next method in the MRO, ensuring proper method chaining.
What is a common problem when multiple parent classes have methods with the same name?
AMethod name conflict
BSyntax error
CInfinite loops
DMemory leaks
✗ Incorrect
When parent classes have methods with the same name, it can cause conflicts about which method is called.
Which design approach can be better than multiple inheritance to reduce complexity?
AComposition
BGlobal variables
CMultiple modules
DRecursion
✗ Incorrect
Composition means using objects inside classes to share behavior, which can be simpler than multiple inheritance.
What does using super() in a method do in a multiple inheritance scenario?
ACalls all parent methods at once
BPrevents method calls
COverrides all parent methods
DCalls the next method in the MRO
✗ Incorrect
super() calls the next method in the MRO, helping to avoid duplicate calls and confusion.
Explain what multiple inheritance is and why it can be tricky to use.
Think about how a class can get features from more than one parent.
You got /4 concepts.
Describe best practices to follow when using multiple inheritance in Python.
Focus on how to keep code clear and avoid bugs.
You got /4 concepts.
Practice
(1/5)
1.
What is the main reason to use super() in multiple inheritance?
easy
A. To create a new instance of the child class
B. To call only the first parent class method
C. To avoid using any parent class methods
D. To ensure all parent classes are properly initialized
Solution
Step 1: Understand the role of super() in multiple inheritance
super() helps call the next method in the method resolution order (MRO), ensuring all parent classes get initialized properly.
Step 2: Recognize why this is important
Without super(), some parent classes might be skipped, causing incomplete initialization.
Final Answer:
To ensure all parent classes are properly initialized -> Option D
Quick Check:
Use super() to call all parents [OK]
Hint: Use super() to call all parents in order [OK]
Common Mistakes:
Calling only one parent class directly
Not using super() causing skipped initializations
Confusing super() with creating new instances
2.
Which of the following is the correct syntax to define a class Child inheriting from Parent1 and Parent2?
?
easy
A. class Child: Parent1, Parent2
B. class Child(Parent1, Parent2):
C. class Child inherits Parent1, Parent2:
D. class Child(Parent1 & Parent2):
Solution
Step 1: Recall Python class inheritance syntax
In Python, multiple inheritance is declared by listing parent classes inside parentheses separated by commas.
Step 2: Match the correct syntax
class Child(Parent1, Parent2): uses class Child(Parent1, Parent2):, which is the correct Python syntax.
Final Answer:
class Child(Parent1, Parent2): -> Option B
Quick Check:
Use parentheses with commas for multiple inheritance [OK]
Hint: Use parentheses with commas for multiple parents [OK]
Common Mistakes:
Using incorrect keywords like 'inherits'
Using '&' instead of commas
Placing parents outside parentheses
3.
What will be the output of the following code?
class A:
def greet(self):
print('Hello from A')
class B(A):
def greet(self):
print('Hello from B')
super().greet()
class C(A):
def greet(self):
print('Hello from C')
super().greet()
class D(B, C):
def greet(self):
print('Hello from D')
super().greet()
d = D()
d.greet()
medium
A. Hello from D
Hello from B
Hello from C
Hello from A
B. Hello from D
Hello from C
Hello from B
Hello from A
C. Hello from D
Hello from B
Hello from A
D. Hello from D
Hello from C
Hello from A
Solution
Step 1: Understand the method resolution order (MRO)
For class D(B, C), the MRO is D > B > C > A. Calling super() follows this order.
Step 2: Trace the calls
d.greet() prints 'Hello from D', then calls B.greet() which prints 'Hello from B' and calls C.greet(). C.greet() prints 'Hello from C' and calls A.greet(), which prints 'Hello from A'.
Final Answer:
Hello from D
Hello from B
Hello from C
Hello from A -> Option A
Quick Check:
MRO order = D, B, C, A [OK]
Hint: Follow MRO order when super() is called [OK]
Common Mistakes:
Ignoring MRO and calling parents in wrong order
Assuming super() calls only immediate parent
Missing one of the parent class prints
4.
Identify the error in the following code snippet using multiple inheritance:
class X:
def __init__(self):
print('X init')
class Y:
def __init__(self):
print('Y init')
class Z(X, Y):
def __init__(self):
X.__init__(self)
Y.__init__(self)
z = Z()
medium
A. Class Z should inherit only from one parent
B. Missing call to super().__init__() in class Z
C. Directly calling parent __init__ methods can cause problems in complex hierarchies
D. No error, code runs fine
Solution
Step 1: Analyze direct calls to parent __init__ methods
Calling X.__init__(self) and Y.__init__(self) directly bypasses Python's MRO and can cause issues if the hierarchy grows complex.
Step 2: Understand best practice
Using super().__init__() respects MRO and avoids duplicate or missed calls.
Final Answer:
Directly calling parent __init__ methods can cause problems in complex hierarchies -> Option C
Quick Check:
Use super() to avoid init call issues [OK]
Hint: Avoid direct parent calls; use super() instead [OK]
Common Mistakes:
Thinking direct calls are always safe
Ignoring MRO and its importance
Believing multiple inheritance requires single parent only
5.
You want to create a class SmartPhone that inherits features from Camera and Phone. Both parents have an __init__ method. How should you design SmartPhone to properly initialize both parents following best practices?
hard
A. Define SmartPhone.__init__ and call super().__init__() only once, relying on parents to use super() too
B. Define SmartPhone.__init__ but leave it empty
C. Do not define __init__ in SmartPhone, parents will initialize automatically
D. Define SmartPhone.__init__ and call Camera.__init__(self) and Phone.__init__(self) directly