Inheriting attributes and methods in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use inheritance in Python, we want to know how it affects the time it takes to run our program.
We ask: How does accessing inherited attributes or methods grow as our program runs?
Analyze the time complexity of accessing inherited attributes and methods.
class Animal:
def speak(self):
return "Sound"
class Dog(Animal):
def speak(self):
return "Bark"
pet = Dog()
print(pet.speak())
This code shows a class Dog inheriting from Animal and overriding the speak method.
Look for repeated actions that affect time.
- Primary operation: Method call pet.speak()
- How many times: Once in this example, but could be many in a program
Accessing an inherited method takes about the same time no matter how many classes are involved.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 method calls, each quick |
| 100 | 100 method calls, each quick |
| 1000 | 1000 method calls, each quick |
Pattern observation: Each method call takes a small, steady amount of time regardless of inheritance depth.
Time Complexity: O(1)
This means calling an inherited method takes a constant amount of time no matter how many times you do it.
[X] Wrong: "Calling an inherited method gets slower if the parent class has many methods or deep inheritance."
[OK] Correct: Python looks up methods efficiently, so the time to find and run a method stays about the same.
Understanding that inheritance does not slow down method calls helps you write clear and efficient code confidently.
What if we added many layers of inheritance? How would the time complexity of calling a method change?
Practice
Solution
Step 1: Understand inheritance basics
Inheritance means the child class gets all features (attributes and methods) of the parent class automatically.Step 2: Analyze each option
The child class automatically has all attributes and methods of the parent class. correctly states this. Options B, C, and D are incorrect because they limit or deny inheritance features.Final Answer:
The child class automatically has all attributes and methods of the parent class. -> Option CQuick Check:
Inheritance = automatic access to parent features [OK]
- Thinking child must redefine parent methods
- Believing child cannot add new features
- Assuming attributes are not inherited
Dog inherit from class Animal in Python?Solution
Step 1: Recall Python inheritance syntax
In Python, inheritance is shown by putting the parent class name in parentheses after the child class name.Step 2: Match syntax to options
class Dog(Animal): usesclass Dog(Animal):which is correct. Others use invalid syntax.Final Answer:
class Dog(Animal): -> Option AQuick Check:
Inheritance syntax = class Child(Parent): [OK]
- Using 'inherits' keyword instead of parentheses
- Using arrow or colon incorrectly
- Omitting parentheses
class Parent:
def greet(self):
return "Hello from Parent"
class Child(Parent):
pass
c = Child()
print(c.greet())Solution
Step 1: Understand method inheritance
Child class inheritsgreetmethod from Parent because it has no owngreet.Step 2: Trace the method call
Callingc.greet()runs Parent'sgreetreturning "Hello from Parent".Final Answer:
Hello from Parent -> Option BQuick Check:
Inherited method runs if child has none [OK]
- Expecting child's own greet method when none exists
- Confusing AttributeError with missing method
- Thinking syntax error occurs
class Animal:
def sound(self):
return "Some sound"
class Dog(Animal):
def sound():
return "Bark"
d = Dog()
print(d.sound())Solution
Step 1: Check method definitions
In Python, instance methods must haveselfas first parameter.Step 2: Identify error in Dog's sound
Dog'ssoundmethod lacksself, causing a TypeError when called on instance.Final Answer:
Missing self parameter in Dog's sound method -> Option DQuick Check:
Instance methods need self parameter [OK]
- Forgetting self in child method
- Thinking inheritance disallows overriding
- Assuming print syntax is wrong
class Vehicle:
def __init__(self, brand):
self.brand = brand
def info(self):
return f"Vehicle brand: {self.brand}"
class Car(Vehicle):
def __init__(self, brand, model):
super().__init__(brand)
self.model = model
def info(self):
return f"Car brand: {self.brand}, model: {self.model}"What will
print(Car('Toyota', 'Corolla').info()) output?Solution
Step 1: Understand constructor chaining
Car's__init__callssuper().__init__(brand)to setbrandin Vehicle.Step 2: Analyze info method override
Car overridesinfoto include bothbrandandmodel.Step 3: Predict output
Callinginfo()on Car instance returns "Car brand: Toyota, model: Corolla".Final Answer:
Car brand: Toyota, model: Corolla -> Option AQuick Check:
Child overrides method and calls parent's init [OK]
- Forgetting super() call in child __init__
- Expecting parent info output instead of child's
- Confusing missing arguments error
