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
Instance Methods in a Bank Account
📖 Scenario: You are creating a simple bank account system. Each account can store the owner's name and balance. You will add methods to deposit and withdraw money.
🎯 Goal: Build a BankAccount class with instance methods to deposit and withdraw money, and then display the current balance.
📋 What You'll Learn
Create a class called BankAccount with an __init__ method
Add an instance method called deposit that adds money to the balance
Add an instance method called withdraw that subtracts money from the balance
Create an instance of BankAccount with owner name and initial balance
Use the instance methods to change the balance
Print the final balance
💡 Why This Matters
🌍 Real World
Bank accounts in real life keep track of money for each person. Instance methods let us add or remove money safely.
💼 Career
Understanding instance methods is key for programming with objects, which is common in software development jobs.
Progress0 / 4 steps
1
Create the BankAccount class with initial data
Create a class called BankAccount with an __init__ method that takes self, owner, and balance. Inside __init__, set self.owner to owner and self.balance to balance. Then create an instance called account with owner 'Alice' and balance 1000.
Python
Hint
Remember, __init__ sets up the object with the owner's name and starting balance.
2
Add a deposit method
Add an instance method called deposit to the BankAccount class. It should take self and amount as parameters and add amount to self.balance.
Python
Hint
The deposit method changes the balance by adding the amount.
3
Add a withdraw method
Add an instance method called withdraw to the BankAccount class. It should take self and amount as parameters and subtract amount from self.balance.
Python
Hint
The withdraw method reduces the balance by the amount.
4
Use methods and print the balance
Use the deposit method on account to add 500. Then use the withdraw method on account to subtract 200. Finally, print account.balance to show the current balance.
Python
Hint
Use the methods on the account object and then print the balance.
Practice
(1/5)
1. What is the purpose of the self parameter in an instance method?
easy
A. It refers to the specific object calling the method.
B. It is used to define a static method.
C. It is a keyword to create a new object.
D. It stores the return value of the method.
Solution
Step 1: Understand what self represents
self is a reference to the current object that calls the method, allowing access to its attributes and other methods.
Step 2: Differentiate from other options
Static methods, object creation, and return values are unrelated concepts, which are not the role of self.
Final Answer:
It refers to the specific object calling the method. -> Option A
Quick Check:
self = current object [OK]
Hint: Remember: self means 'this object' inside methods [OK]
Common Mistakes:
Thinking self is a keyword, not a parameter
Confusing self with class or static methods
Assuming self is optional in instance methods
2. Which of the following is the correct way to define an instance method inside a Python class?
easy
A. def method_name():
B. def method_name(*args):
C. def method_name(cls):
D. def method_name(self):
Solution
Step 1: Recall instance method syntax
Instance methods must have self as the first parameter to access the object's data.
Step 2: Check each option
def method_name(): misses self, def method_name(cls): uses cls which is for class methods, and def method_name(*args): uses a generic parameter which is not standard for instance methods.
Final Answer:
def method_name(self): -> Option D
Quick Check:
Instance method = first param self [OK]
Hint: Instance methods always start with self parameter [OK]
Common Mistakes:
Omitting self in method definition
Using cls instead of self for instance methods
Using no parameters or *args incorrectly
3. What will be the output of this code?
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return f"{self.name} says Woof!"
my_dog = Dog('Buddy')
print(my_dog.bark())
medium
A. Woof!
B. Buddy says Woof!
C. my_dog says Woof!
D. Error: missing self parameter
Solution
Step 1: Understand object creation and method call
The object my_dog is created with name 'Buddy'. Calling bark() uses self.name which is 'Buddy'.
Step 2: Evaluate the return value
The method returns the string "Buddy says Woof!" which is printed.
Final Answer:
Buddy says Woof! -> Option B
Quick Check:
Method uses self.name = Buddy [OK]
Hint: Instance methods use self to access object data [OK]
Common Mistakes:
Ignoring self and expecting just 'Woof!'
Confusing variable name with object name
Assuming method returns nothing
4. Find the error in this code:
class Car:
def __init__(self, model):
self.model = model
def show_model():
print(f"Model: {self.model}")
car = Car('Tesla')
car.show_model()
medium
A. Cannot create object without arguments
B. Wrong attribute name used
C. Missing self parameter in show_model method
D. print statement syntax error
Solution
Step 1: Check method definition
The method show_model is missing the self parameter, so it cannot access instance attributes.
Step 2: Understand the error cause
Calling car.show_model() passes the object automatically, but method lacks self to receive it, causing a TypeError.
Final Answer:
Missing self parameter in show_model method -> Option C
Quick Check:
Instance methods need self parameter [OK]
Hint: Always include self as first parameter in instance methods [OK]
Common Mistakes:
Forgetting self in method definition
Trying to access self without parameter
Confusing class and instance methods
5. You want to create a class Counter that counts how many times its method increment is called on each object separately. Which code correctly implements this behavior?
hard
A. class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count += 1
def get_count(self):
return self.count
B. class Counter:
count = 0
def increment(self):
Counter.count += 1
def get_count(self):
return Counter.count
C. class Counter:
def __init__(self):
self.count = 0
def increment():
self.count += 1
def get_count(self):
return self.count
D. class Counter:
def __init__(self):
self.count = 0
def increment(self):
count += 1
def get_count(self):
return self.count
Solution
Step 1: Understand instance vs class variables
Instance variables (self.count) ensure each object tracks its own count separately. Methods must accept self and update self.count.
Step 2: Eliminate incorrect approaches
Class variables are shared across all instances. Missing self parameter in methods causes TypeError. Updating a local variable doesn't affect the instance attribute.
Final Answer:
class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count += 1
def get_count(self):
return self.count -> Option A
Quick Check:
Instance variables + self = separate counts [OK]
Hint: Use self.variable for per-object data, not class variables [OK]
Common Mistakes:
Using class variables for per-object data
Forgetting self in method parameters
Incrementing local variables instead of instance attributes