Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Modifying Object State in Python
📖 Scenario: You are creating a simple program to manage a bank account. The account has a balance that can change when you deposit or withdraw money.
🎯 Goal: Build a Python class called BankAccount that can store a balance and update it when deposits or withdrawals happen.
📋 What You'll Learn
Create a class named BankAccount with an attribute balance.
Add a method deposit that increases the balance by a given amount.
Add a method withdraw that decreases the balance by a given amount.
Print the final balance after some deposits and withdrawals.
💡 Why This Matters
🌍 Real World
Managing bank accounts or any system where you track and update values over time.
💼 Career
Understanding how to modify object state is key for software development, especially in building interactive applications and managing data.
Progress0 / 4 steps
1
Create the BankAccount class with initial balance
Create a class called BankAccount with an __init__ method that sets the balance attribute to 0.
Python
Hint
Use self.balance = 0 inside the __init__ method to set the starting balance.
2
Add a deposit method to increase balance
Add a method called deposit to the BankAccount class that takes a parameter amount and adds it to self.balance.
Python
Hint
Use self.balance += amount inside the deposit method to add money.
3
Add a withdraw method to decrease balance
Add a method called withdraw to the BankAccount class that takes a parameter amount and subtracts it from self.balance.
Python
Hint
Use self.balance -= amount inside the withdraw method to remove money.
4
Create an account, deposit, withdraw, and print balance
Create an instance of BankAccount called account. Use the deposit method to add 100, then use the withdraw method to remove 30. Finally, print account.balance.
Python
Hint
Remember to create the object, call deposit(100), then withdraw(30), and print the balance.
Practice
(1/5)
1. What does it mean to modify the state of an object in Python?
easy
A. Changing the values of its attributes
B. Creating a new object
C. Deleting the object
D. Printing the object
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 A
Quick Check:
Modify state = change attribute values [OK]
Hint: State means attribute values; changing them modifies state [OK]
Common Mistakes:
Confusing creating a new object with modifying state
Thinking printing changes state
Mixing deleting object with modifying state
2. Which of the following is the correct way to change an object's attribute color to 'blue'?
easy
A. object.color('blue')
B. object->color = 'blue'
C. object.color = 'blue'
D. object[color] = '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 C
Quick Check:
Use dot and = to assign attribute [OK]
Hint: Use dot and equals to set attribute: object.attr = value [OK]
Common Mistakes:
Using brackets [] instead of dot for attributes
Using arrow -> which is not Python syntax
Trying to call attribute like a function
3. What will be the output of this code?
class Box:
def __init__(self):
self.size = 5
def enlarge(self):
self.size += 3
b = Box()
b.enlarge()
print(b.size)
medium
A. 5
B. 8
C. 3
D. Error
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 B
Quick Check:
5 + 3 = 8 [OK]
Hint: Add changes inside method to attribute value [OK]
Common Mistakes:
Forgetting method changes attribute
Thinking size resets after method call
Expecting error due to method call
4. Find the error in this code that tries to update an object's attribute:
class Car:
def __init__(self):
self.speed = 0
def accelerate(self):
speed += 10
c = Car()
c.accelerate()
print(c.speed)
medium
A. Using speed without self inside accelerate method
B. Missing self in accelerate method parameter
C. Incorrect print statement syntax
D. No error, code runs fine
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 A
Quick Check:
Use self.speed to modify attribute [OK]
Hint: Always use self.attribute to change object state inside methods [OK]
Common Mistakes:
Forgetting self. before attribute inside methods
Thinking print syntax is wrong
Assuming missing self parameter causes error here
5. You want to keep track of how many times a method use() is called on an object. Which is the best way to modify the object state to do this?
hard
A. Delete the object after each use() call
B. Print a message every time use() is called
C. Create a new object each time use() is called
D. Add an attribute count initialized to 0 and increase it inside use()
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 D
Quick Check:
Use attribute to track count [OK]
Hint: Use attribute counter updated inside method to track calls [OK]