OOP principles overview in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use object-oriented programming (OOP), we organize code into objects and classes. It's important to understand how the time it takes to run our programs changes as we use these OOP ideas.
We want to see how the main actions in OOP affect the program's speed when the program grows bigger.
Analyze the time complexity of the following code snippet.
class Item:
def __init__(self, value):
self.value = value
class Container:
def __init__(self, items):
self.items = items
def total_value(self):
total = 0
for item in self.items:
total += item.value
return total
This code defines two classes. One holds a value, and the other holds many such items and sums their values.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of items in
total_value. - How many times: Once for each item in the container.
As the number of items grows, the time to add their values grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items. Double the items, double the work.
Time Complexity: O(n)
This means the time to run total_value grows in a straight line with the number of items.
[X] Wrong: "Using classes always makes the program slower because of extra steps."
[OK] Correct: Classes organize code but do not add hidden loops. The main time depends on what the code inside the methods does, like loops over data.
Understanding how loops inside class methods affect time helps you explain your code clearly. It shows you know how your program grows and runs efficiently.
"What if the total_value method called another method inside each item that also loops over data? How would the time complexity change?"
Practice
Solution
Step 1: Recall the four main OOP principles
The four main principles are Encapsulation, Inheritance, Polymorphism, and Abstraction.Step 2: Identify the option not in the list
Compilation is a process related to converting code, not an OOP principle.Final Answer:
Compilation -> Option AQuick Check:
OOP principles exclude Compilation [OK]
- Confusing compilation with OOP concepts
- Mixing up abstraction with compilation
- Thinking all programming terms are OOP principles
Solution
Step 1: Recall Python syntax for defining classes
In Python, the keywordclassis used to define a new class.Step 2: Check other options
defdefines functions,objectis a base class, andfuncis not a Python keyword.Final Answer:
class -> Option DQuick Check:
Use 'class' to define classes [OK]
- Using def instead of class for classes
- Confusing object with class keyword
- Trying to use func which is invalid
class Animal:
def speak(self):
return "Sound"
class Dog(Animal):
def speak(self):
return "Bark"
pet = Dog()
print(pet.speak())Solution
Step 1: Understand inheritance and method overriding
Dog class inherits from Animal and overrides the speak method to return "Bark".Step 2: Check which speak method is called
pet is an instance of Dog, so pet.speak() calls Dog's speak method, returning "Bark".Final Answer:
Bark -> Option AQuick Check:
Overridden method returns 'Bark' [OK]
- Expecting parent class method output
- Confusing method overriding with overloading
- Thinking print outputs None
class Car:
def __init__(self, model):
self.model = model
def display(self):
print(Model)Solution
Step 1: Check the print statement inside display method
The print statement usesModelwhich is undefined; it should useself.modelto access the instance variable.Step 2: Verify other parts
The constructor name__init__is correct, and method has self parameter. Class name capitalization is fine.Final Answer:
Model should be self.model in print -> Option CQuick Check:
Use self to access instance variables [OK]
- Forgetting self in method parameters
- Using variable name without self prefix
- Thinking constructor name is incorrect
Solution
Step 1: Understand the principle of hiding data
Hiding internal data and controlling access through methods is called Encapsulation.Step 2: Differentiate from other principles
Inheritance is about reusing code, Polymorphism is about using methods in different ways, Abstraction is about hiding complexity but not necessarily data.Final Answer:
Encapsulation -> Option BQuick Check:
Data hiding = Encapsulation [OK]
- Confusing encapsulation with abstraction
- Mixing inheritance with data hiding
- Thinking polymorphism hides data
