As I continue my Python learning journey, I recently reached one of the concepts I was really looking forward to: Classes and Objects.
At first, terms like class, object, instance, method, and self felt a little confusing.
So instead of only reading about them, I decided to build something practical:
🏦 A Bank Account Management System
This project helped me understand how classes and objects actually work in a real program.
So, what is a class?
The simplest way I can explain it is:
A class is a blueprint.
Imagine a bank has a standard design for every customer account.
Every account might have:
- An owner
- A balance
- A way to deposit money
- A way to withdraw money
- A way to check the balance
- A way to transfer money
We can represent that structure using a Python class:
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self.balance = balance
The class describes what a bank account should have and what it should be able to do.
Then what is an object?
An object is an actual instance created from the class.
For example:
account1 = BankAccount("Israel", 10000)
account2 = BankAccount("John", 5000)
Here, BankAccount is the class, while account1 and account2 are objects.
Both objects were created from the same blueprint, but they have different data.
print(account1.owner)
# Israel
print(account2.owner)
# John
This was one of the moments where the concept really started to make sense to me.
Understanding self
One thing I had to spend time understanding was self.
When I write:
self.owner = owner
self.balance = balance
self refers to the specific object currently being worked with.
So when I do:
account1.deposit(20000)
Python knows that self refers to account1.
And when I do:
account2.deposit(2000)
self refers to account2.
That means each object can maintain its own data.
Adding methods
I then added methods to make the bank account actually useful.
For example, the deposit() method:
def deposit(self, amount):
if amount <= 0:
raise ValueError("Invalid amount")
else:
self.balance += amount
return f"Deposit of {amount} made. New balance is {self.balance}"
I also created methods for:
- Depositing money
- Withdrawing money
- Checking the balance
- Transferring money between accounts
The transfer method was especially interesting:
def transfer(self, account, amount):
if amount <= 0:
raise ValueError("Invalid amount")
elif amount > self.balance:
return "Insufficient funds"
else:
self.balance -= amount
account.balance += amount
return f"The sum of {amount} has been transferred to {account.owner}'s account"
This helped me understand that objects can interact with other objects.
For example:
account1.transfer(account2, 3000)
Here:
-
self→account1 -
account→account2 -
amount→3000
So Python subtracts the money from one account and adds it to the other.
I also practiced error handling
I used raise ValueError to prevent invalid transactions.
For example:
try:
account1.deposit(-500)
except ValueError as error:
print(error)
This helped reinforce something I had previously learned about Python error handling.
I also learned the difference between returning a message and raising an exception.
For example:
return "Insufficient funds"
means the method returns a normal value.
While:
raise ValueError("Invalid amount")
means an exception has occurred and needs to be handled.
What I learned from this project
This project helped me understand several important Python concepts:
✅ Classes and objects
✅ __init__()
✅ Instance attributes
✅ Methods
✅ self
✅ Creating multiple objects from one class
✅ Objects interacting with other objects
✅ return inside methods
✅ raise ValueError
✅ try and except
✅ Basic Object-Oriented Programming
The biggest lesson for me was that learning programming becomes much easier when I build something instead of only watching tutorials.
I'm still learning and I definitely have more to understand about Object-Oriented Programming, but building this project made classes and objects much less intimidating.
Next, I'll continue building more projects and gradually move into more advanced OOP concepts.
One project at a time. One concept at a time.
Top comments (0)