Super function usage in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how using the super() function affects the time it takes for a program to run.
Specifically, we ask: does calling super() add extra work as the program grows?
Analyze the time complexity of the following code snippet.
class Base:
def process(self, data):
return [x * 2 for x in data]
class Child(Base):
def process(self, data):
result = super().process(data)
return [x + 1 for x in result]
items = list(range(1000))
child = Child()
output = child.process(items)
This code doubles each number in a list using the base class, then adds one to each number in the child class.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two list comprehensions that each go through the entire list.
- How many times: Each list comprehension runs once, but each touches every item in the input list.
As the input list gets bigger, the program does more work because it processes every item twice.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (10 doubled, 10 added) |
| 100 | About 200 operations |
| 1000 | About 2000 operations |
Pattern observation: The work grows roughly twice as fast as the input size because of two passes over the data.
Time Complexity: O(n)
This means the time to run grows in a straight line with the size of the input list, even with super() calls.
[X] Wrong: "Using super() makes the program slower by a lot because it adds extra hidden loops."
[OK] Correct: super() just calls the parent method once; it does not add extra loops beyond what the parent method already does.
Understanding how super() affects time helps you explain your code clearly and shows you know how inheritance impacts performance.
What if the parent method also called super() in a longer inheritance chain? How would that affect the time complexity?
Practice
super() in a child class?Solution
Step 1: Understand what
super()doessuper()is used to access methods from the parent class inside a child class.Step 2: Identify the correct purpose
Calling a parent class method helps reuse code and extend functionality.Final Answer:
To call a method from the parent class -> Option AQuick Check:
super() calls parent method = A [OK]
- Thinking super() creates new objects
- Believing super() deletes classes
- Assuming super() overrides child methods fully
greet inside a child class method using super()?Solution
Step 1: Recall the syntax of super()
The correct way to call a parent method is usingsuper()followed by dot and method name.Step 2: Match the correct option
Onlysuper().greet()uses the right parentheses and dot notation.Final Answer:
super().greet() -> Option AQuick Check:
Use parentheses with super() = D [OK]
- Omitting parentheses after super
- Using square brackets instead of parentheses
- Using arrow notation which is invalid in Python
class Parent:
def greet(self):
return "Hello from Parent"
class Child(Parent):
def greet(self):
return super().greet() + " and Child"
c = Child()
print(c.greet())Solution
Step 1: Understand method calls in Child.greet()
Child's greet callssuper().greet()which runs Parent's greet returning Hello from Parent.Step 2: Combine returned strings
Child's greet adds and Child to the parent's string, so final output is Hello from Parent and Child.Final Answer:
Hello from Parent and Child -> Option CQuick Check:
super() calls parent method + extra text = A [OK]
- Expecting only parent's message without child addition
- Thinking super() causes error here
- Ignoring the string concatenation
super():
class Base:
def show(self):
print("Base show")
class Derived(Base):
def show(self):
super.show()
print("Derived show")
d = Derived()
d.show()Solution
Step 1: Identify how super() is called
The code usessuper.show()which is invalid 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 DQuick Check:
super() needs parentheses before method = C [OK]
- Calling super without parentheses
- Thinking parent method is missing
- Believing overriding is not allowed
__init__ method to add a new attribute in the child class. Which code correctly uses super() to do this?
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
???
self.breed = breed
Choose the correct replacement for ???.Solution
Step 1: Understand parent __init__ parameters
Animal's __init__ takesname, so we must passnameto it.Step 2: Use super() correctly in child __init__
Callingsuper().__init__(name)runs Animal's __init__ properly, then child addsbreed.Final Answer:
super().__init__(name) -> Option BQuick Check:
super() calls parent with correct args = B [OK]
- Passing wrong argument to super()
- Calling parent __init__ without self
- Using old super() syntax incorrectly
