Multiple inheritance syntax in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's explore how the time needed to create a class with multiple inheritance grows as we add more parent classes.
We want to see how the program's work changes when using multiple inheritance syntax.
Analyze the time complexity of the following code snippet.
class A:
def method(self):
print("A method")
class B:
def method(self):
print("B method")
class C(A, B):
pass
obj = C()
obj.method()
This code defines two parent classes and one child class that inherits from both. It then creates an object and calls a method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Python looks through the parent classes in order to find the method.
- How many times: It checks each parent class once in the order they are listed.
When the program looks for a method, it checks each parent class one by one until it finds it.
| Number of Parent Classes (n) | Approx. Checks |
|---|---|
| 2 | Up to 2 checks |
| 5 | Up to 5 checks |
| 10 | Up to 10 checks |
Pattern observation: The number of checks grows directly with the number of parent classes.
Time Complexity: O(n)
This means the time to find a method grows linearly with the number of parent classes.
[X] Wrong: "Multiple inheritance always makes method lookup instant because Python knows all parents at once."
[OK] Correct: Python checks parent classes one by one in order, so more parents mean more checks.
Understanding how multiple inheritance affects method lookup helps you explain how Python manages class relationships and performance.
"What if the child class overrides the method? How would that change the time complexity of method lookup?"
Practice
Child that inherits from two parent classes Parent1 and Parent2 in Python?Solution
Step 1: Understand Python class inheritance syntax
In Python, to inherit from multiple classes, list them separated by commas inside parentheses after the class name.Step 2: Match the syntax to the options
class Child(Parent1, Parent2): uses the correct syntax:class Child(Parent1, Parent2):. Other options use invalid syntax.Final Answer:
class Child(Parent1, Parent2): -> Option AQuick Check:
Multiple inheritance syntax = class Child(Parent1, Parent2): [OK]
- Using '&' instead of commas between parent classes
- Writing 'inherits' keyword like other languages
- Not using parentheses after class name
Solution
Step 1: Check syntax for multiple inheritance
Parent classes must be separated by commas inside parentheses.Step 2: Identify the incorrect option
class MyClass(Parent1 Parent2): pass misses the comma between Parent1 and Parent2, causing a syntax error.Final Answer:
class MyClass(Parent1 Parent2): pass -> Option DQuick Check:
Missing comma between parents = SyntaxError [OK]
- Omitting commas between parent classes
- Leaving out parentheses entirely
- Using colons instead of commas
class A:
def greet(self):
return "Hello from A"
class B:
def greet(self):
return "Hello from B"
class C(A, B):
pass
obj = C()
print(obj.greet())Solution
Step 1: Understand method resolution order (MRO)
Class C inherits from A first, then B. Python looks for methods in the order of parents listed.Step 2: Determine which greet() is called
Since A is first, C uses A's greet method, returning "Hello from A".Final Answer:
"Hello from A" -> Option AQuick Check:
MRO follows parent order = "Hello from A" [OK]
- Assuming last parent class method is called
- Expecting an error due to multiple parents
- Confusing method names or forgetting MRO
class X:
pass
class Y:
pass
class Z(X Y):
passSolution
Step 1: Check syntax for multiple inheritance
Parent classes must be separated by commas inside parentheses.Step 2: Identify the syntax error
In class Z(X Y):, the comma between X and Y is missing, causing a syntax error.Final Answer:
Missing comma between parent classes -> Option BQuick Check:
Comma missing between parents = SyntaxError [OK]
- Forgetting commas between parent classes
- Using square brackets instead of parentheses
- Thinking multiple inheritance is not allowed
class Alpha:
def action(self):
return "Alpha"
class Beta:
def action(self):
return "Beta"
class Gamma(Alpha, Beta):
def action(self):
return super().action() + " & Gamma"
class Delta(Gamma, Beta):
pass
obj = Delta()
print(obj.action())What is the output?
Solution
Step 1: Understand the inheritance chain and MRO
Delta inherits from Gamma and Beta. Gamma inherits from Alpha and Beta. The MRO for Delta is Delta, Gamma, Alpha, Beta, object.Step 2: Trace the action() method call
Delta uses Gamma's action(), which calls super().action(). In Gamma, super() refers to Alpha (next in MRO), so Alpha.action() returns "Alpha". Then Gamma appends " & Gamma".Final Answer:
"Alpha & Gamma" -> Option CQuick Check:
super() follows MRO = "Alpha & Gamma" [OK]
- Assuming super() calls Beta's method instead of Alpha's
- Ignoring MRO order in multiple inheritance
- Expecting an error due to complex inheritance
