Extending parent behavior in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we extend parent behavior in programming, we often add new actions on top of existing ones.
We want to see how this affects the time it takes for the program to run as the input grows.
Analyze the time complexity of the following code snippet.
class Parent:
def process(self, data):
for item in data:
print(item)
class Child(Parent):
def process(self, data):
super().process(data)
for item in data:
print(item * 2)
This code shows a child class extending a parent's method by first running the parent's loop, then adding its own loop over the same data.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops over the input list.
- How many times: Each loop runs once over all items in the input.
As the input list gets bigger, the program runs two loops, each touching every item once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (2 loops x 10 items) |
| 100 | About 200 operations |
| 1000 | About 2000 operations |
Pattern observation: The total work grows roughly twice as fast as the input size, but still in a straight line.
Time Complexity: O(n)
This means the time to run grows directly in proportion to the size of the input list.
[X] Wrong: "Because there are two loops, the time complexity is squared, like O(n²)."
[OK] Correct: The loops run one after another, not inside each other, so their times add up, not multiply.
Understanding how extending behavior affects time helps you explain your code clearly and shows you can think about efficiency in real projects.
"What if the child class added a nested loop inside its process method? How would the time complexity change?"
Practice
super() do in a child class method?Solution
Step 1: Understand the role of
super()super()is used to call a method from the parent class inside a child class method.Step 2: Recognize code reuse
By calling the parent method, the child can reuse existing behavior and add new features without rewriting code.Final Answer:
It calls the parent class method to reuse its behavior. -> Option BQuick Check:
super() calls parent method = D [OK]
- Thinking super() creates new instances
- Believing super() deletes methods
- Assuming super() overrides without calling parent
greet inside a child class method in Python?Solution
Step 1: Recall correct super() syntax
In Python,super()is a function and must be called with parentheses before accessing methods.Step 2: Identify correct method call
The correct way to call the parent method issuper().greet(), notsuper.greet()or others.Final Answer:
super().greet() -> Option AQuick Check:
super() needs parentheses = A [OK]
- Omitting parentheses after super
- Using parent instead of super
- Trying to access super as an attribute
class Parent:
def greet(self):
print('Hello from Parent')
class Child(Parent):
def greet(self):
super().greet()
print('Hello from Child')
c = Child()
c.greet()Solution
Step 1: Trace the child greet() method
The child method callssuper().greet()first, which prints 'Hello from Parent'.Step 2: Continue child method execution
After calling the parent method, it prints 'Hello from Child'. So both lines print in order.Final Answer:
Hello from Parent Hello from Child -> Option DQuick Check:
super() calls parent then child prints = B [OK]
- Ignoring the parent print
- Expecting only child output
- Thinking super() causes error
class Parent:
def show(self):
print('Parent show')
class Child(Parent):
def show(self):
super.show()
print('Child show')Solution
Step 1: Check super() usage
The code usessuper.show()which is incorrect syntax;super()must be called as a function.Step 2: Correct the syntax
It should besuper().show()to properly call the parent method.Final Answer:
super.show() should be super().show() -> Option AQuick Check:
super() needs parentheses to call methods = A [OK]
- Forgetting parentheses after super
- Thinking parent method needs no self
- Believing print must be in __init__
calculate so that the child class adds 10 to the parent's result. Which code correctly does this?
class Parent:
def calculate(self):
return 5
class Child(Parent):
def calculate(self):
# Fill here
Solution
Step 1: Use super() to call parent method
To get the parent's result, callsuper().calculate()inside the child method.Step 2: Add 10 to the parent's result
Return the parent's value plus 10 assuper().calculate() + 10.Final Answer:
return super().calculate() + 10 -> Option CQuick Check:
super() calls parent, add 10 = C [OK]
- Calling calculate() without super causes recursion
- Calling Parent.calculate() without instance
- Using self.calculate() causes infinite loop
