Modifying object state in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we change the state of an object, we want to know how long it takes as the object grows.
We ask: how does the time to update the object change with its size?
Analyze the time complexity of the following code snippet.
class Counter:
def __init__(self):
self.counts = {}
def add(self, key):
if key in self.counts:
self.counts[key] += 1
else:
self.counts[key] = 1
This code updates a dictionary inside an object by increasing the count for a given key.
- Primary operation: Checking if a key exists and updating the dictionary.
- How many times: Each call updates one key once; repeated calls happen outside this snippet.
Each update looks up a key and changes its value. The time depends on how fast the dictionary can find the key.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 operation per update |
| 100 | Still about 1 operation per update |
| 1000 | Still about 1 operation per update |
Pattern observation: The time to update stays roughly the same no matter how many keys are stored.
Time Complexity: O(1)
This means updating the object's state takes about the same time no matter how many keys it has.
[X] Wrong: "Updating the count takes longer as the dictionary grows because it has more keys."
[OK] Correct: Dictionaries use fast lookups that keep update time steady, so size does not slow down each update.
Understanding how object state changes scale helps you explain efficiency clearly and confidently in real coding talks.
"What if the counts were stored in a list instead of a dictionary? How would the time complexity change?"
Practice
Solution
Step 1: Understand object state
The state of an object is stored in its attributes (variables inside the object).Step 2: What modifying state means
Modifying state means changing these attribute values to new ones.Final Answer:
Changing the values of its attributes -> Option AQuick Check:
Modify state = change attribute values [OK]
- Confusing creating a new object with modifying state
- Thinking printing changes state
- Mixing deleting object with modifying state
color to 'blue'?Solution
Step 1: Attribute assignment syntax
In Python, to change an attribute, use dot notation: object.attribute = value.Step 2: Check each option
object.color = 'blue' uses correct dot notation. Options A, B, and C use invalid syntax for attribute assignment.Final Answer:
object.color = 'blue' -> Option CQuick Check:
Use dot and = to assign attribute [OK]
- Using brackets [] instead of dot for attributes
- Using arrow -> which is not Python syntax
- Trying to call attribute like a function
class Box:
def __init__(self):
self.size = 5
def enlarge(self):
self.size += 3
b = Box()
b.enlarge()
print(b.size)Solution
Step 1: Initial attribute value
The Box object b starts with size = 5 from __init__.Step 2: Method enlarge changes size
Calling b.enlarge() adds 3 to size, so size becomes 5 + 3 = 8.Final Answer:
8 -> Option BQuick Check:
5 + 3 = 8 [OK]
- Forgetting method changes attribute
- Thinking size resets after method call
- Expecting error due to method call
class Car:
def __init__(self):
self.speed = 0
def accelerate(self):
speed += 10
c = Car()
c.accelerate()
print(c.speed)Solution
Step 1: Check method parameter
accelerate has self parameter, so it can access attributes.Step 2: Identify attribute update
Inside accelerate, speed += 10 tries to update speed but misses self. It should be self.speed += 10.Final Answer:
Using speed without self inside accelerate method -> Option AQuick Check:
Use self.speed to modify attribute [OK]
- Forgetting self. before attribute inside methods
- Thinking print syntax is wrong
- Assuming missing self parameter causes error here
use() is called on an object. Which is the best way to modify the object state to do this?Solution
Step 1: Understand tracking calls
To count calls, store a number in the object that updates each time.Step 2: Modify state properly
Initialize an attribute count = 0, then increase it by 1 inside use() method.Final Answer:
Add an attribute count initialized to 0 and increase it inside use() -> Option DQuick Check:
Use attribute to track count [OK]
- Just printing without storing count
- Creating new objects instead of updating state
- Deleting object removes all state
