Public attributes in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with public attributes in Python classes, it's helpful to understand how accessing or modifying these attributes affects the program's speed.
We want to know how the time to get or set a public attribute changes as the program runs.
Analyze the time complexity of the following code snippet.
class Car:
def __init__(self, color):
self.color = color # public attribute
car = Car('red')
print(car.color)
car.color = 'blue'
print(car.color)
This code creates a Car object with a public attribute color, then reads and changes it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing or setting the public attribute
color. - How many times: Each access or assignment happens once here, but could happen many times in a bigger program.
Accessing or changing a public attribute takes the same amount of time no matter how many objects or attributes exist.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 attribute accesses or sets |
| 100 | 100 attribute accesses or sets |
| 1000 | 1000 attribute accesses or sets |
Pattern observation: Each attribute access or change takes the same time, so the total time grows directly with how many times you do it.
Time Complexity: O(1)
This means accessing or changing a public attribute takes a constant amount of time, no matter how big your program or data is.
[X] Wrong: "Accessing a public attribute gets slower if I have many objects or attributes."
[OK] Correct: Each attribute access looks up just one value and does not depend on how many objects or attributes exist elsewhere.
Understanding that attribute access is fast and constant helps you write clear and efficient code, which is a valuable skill in programming and interviews.
"What if we changed the public attribute to a property with a method behind it? How would the time complexity change?"
Practice
Solution
Step 1: Understand attribute visibility
Public attributes are designed to be accessed and modified from outside the class.Step 2: Compare options
An attribute that can be accessed and changed from outside the class correctly describes public attributes. Options A, B, and D describe other concepts.Final Answer:
An attribute that can be accessed and changed from outside the class -> Option AQuick Check:
Public attribute = accessible outside [OK]
- Confusing public with private attributes
- Thinking methods are attributes
- Mixing up constructors with attributes
name inside a Python class constructor?Solution
Step 1: Recall syntax for public attributes
Inside __init__, public attributes are created by assigning to self.attribute_name.Step 2: Check each option
self.name = value uses correct syntax: self.name = value. Others are invalid Python syntax or wrong usage.Final Answer:
self.name = value -> Option BQuick Check:
Use self.attribute = value [OK]
- Using def instead of assignment
- Wrong arrow syntax like self->name
- Assigning attribute to name instead of self.name
class Dog:
def __init__(self, name):
self.name = name
my_dog = Dog('Buddy')
print(my_dog.name)Solution
Step 1: Understand attribute assignment
The constructor sets self.name to the value passed, which is 'Buddy'.Step 2: Print the attribute value
Printing my_dog.name outputs 'Buddy'.Final Answer:
Buddy -> Option DQuick Check:
Print attribute value = Buddy [OK]
- Expecting class name instead of attribute value
- Confusing object name with attribute
- Thinking it causes an error
age:class Person:
def __init__(self, age):
age = age
p = Person(30)
print(p.age)Solution
Step 1: Check attribute assignment
Assigning age = age only creates a local variable, not an attribute of the object.Step 2: Correct attribute assignment
It should be self.age = age to create a public attribute accessible outside.Final Answer:
The attribute should be assigned to self.age, not age -> Option AQuick Check:
Use self.attribute = value to create public attribute [OK]
- Assigning to local variable instead of self.attribute
- Trying to print variable not attached to object
- Thinking constructor needs return
Car that stores the public attributes make and year. Which code correctly creates these attributes and allows access to them?Solution
Step 1: Check attribute assignments
class Car: def __init__(self, make, year): self.make = make self.year = year my_car = Car('Toyota', 2020) print(my_car.make, my_car.year) correctly assigns self.make and self.year to the passed values.Step 2: Check attribute access
class Car: def __init__(self, make, year): self.make = make self.year = year my_car = Car('Toyota', 2020) print(my_car.make, my_car.year) prints my_car.make and my_car.year, which are valid public attributes.Final Answer:
class Car: def __init__(self, make, year): self.make = make self.year = year my_car = Car('Toyota', 2020) print(my_car.make, my_car.year) -> Option CQuick Check:
Assign and access via self.attribute and object.attribute [OK]
- Assigning attributes backwards (make = self.make)
- Accessing attributes without object prefix
- Mixing local variables with attributes
