Accessing and modifying attributes in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we access or change attributes in Python objects, it is important to know how the time needed grows as we do more operations.
We want to understand how fast or slow these attribute actions happen as the program runs.
Analyze the time complexity of the following code snippet.
class Person:
def __init__(self, name):
self.name = name
p = Person("Alice")
for i in range(1000):
p.name = f"Name{i}"
current_name = p.name
This code creates a person and then changes and reads the name attribute 1000 times in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing and modifying the
nameattribute of the object. - How many times: 1000 times inside the loop.
Each loop step does a simple attribute change and access, which takes about the same time no matter how many times we do it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 attribute actions (10 sets + 10 gets) |
| 100 | 200 attribute actions |
| 1000 | 2000 attribute actions |
Pattern observation: The total work grows directly with the number of loop steps.
Time Complexity: O(n)
This means the time needed grows in a straight line as we do more attribute accesses and changes.
[X] Wrong: "Accessing or changing an attribute takes longer each time because the object gets bigger."
[OK] Correct: Attribute access and modification in Python objects happen in constant time, so the object size does not slow down these operations.
Understanding how attribute access scales helps you explain how your code handles data efficiently, a useful skill in many programming tasks.
"What if we added a loop inside the attribute setter that searched a list each time? How would the time complexity change?"
Practice
color of an object car in Python?Solution
Step 1: Understand attribute access syntax
In Python, attributes of an object are accessed using dot notation:object.attribute.Step 2: Apply to given object and attribute
For objectcarand attributecolor, the correct syntax iscar.color.Final Answer:
car.color -> Option AQuick Check:
Dot notation accesses attributes = car.color [OK]
- Using brackets like car[color]
- Using arrow notation like car->color
- Calling attribute as a function like car[color()]
age of an object person to 30?Solution
Step 1: Recall attribute assignment syntax
To modify an attribute, use dot notation with assignment:object.attribute = value.Step 2: Apply to given object and attribute
Setperson.ageto 30 by writingperson.age = 30.Final Answer:
person.age = 30 -> Option BQuick Check:
Assign attribute with dot and equals = person.age = 30 [OK]
- Using brackets like person["age"] = 30
- Using arrow notation person->age = 30
- Trying to call attribute like a function person.age(30)
class Dog:
def __init__(self, name):
self.name = name
my_dog = Dog("Buddy")
print(my_dog.name)
my_dog.name = "Max"
print(my_dog.name)Solution
Step 1: Understand initial attribute value
The constructor setsself.nameto "Buddy". So,my_dog.nameis initially "Buddy".Step 2: Modify attribute and print again
After printing "Buddy", the code setsmy_dog.name = "Max". The second print outputs "Max".Final Answer:
Buddy Max -> Option DQuick Check:
Initial then changed attribute prints = Buddy Max [OK]
- Thinking attribute change does not affect output
- Confusing order of prints
- Assuming attribute is immutable
class Cat:
def __init__(self, color):
self.color = color
kitty = Cat("black")
print(kitty[color])Solution
Step 1: Check attribute access syntax
The code useskitty[color], which tries to access like a dictionary key, butcoloris an attribute, not a key.Step 2: Correct syntax for attribute access
Use dot notation:kitty.colorto access the attribute.Final Answer:
Using brackets instead of dot to access attribute -> Option AQuick Check:
Attributes use dot, not brackets = Using brackets instead of dot to access attribute [OK]
- Using brackets like kitty[color]
- Thinking attributes are dictionary keys
- Confusing attribute access with indexing
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
book1 = Book("1984", "Orwell")
book2 = Book("Animal Farm", "Orwell")
# Change author of book2 to "George Orwell"Which code correctly updates
book2 author without affecting book1?Solution
Step 1: Understand instance vs class attributes
Changingbook2.authormodifies only that instance's attribute, notbook1.Step 2: Avoid changing class attribute or other instance
AssigningBook.authorchanges class attribute for all instances; changingbook1.authoraffects wrong object; brackets are invalid syntax.Final Answer:
book2.author = "George Orwell" -> Option CQuick Check:
Set instance attribute with dot on correct object = book2.author = "George Orwell" [OK]
- Changing class attribute instead of instance attribute
- Modifying wrong object's attribute
- Using brackets instead of dot notation
