Python > Object-Oriented Programming (OOP) in Python > Classes and Objects > Methods (Instance Methods, Class Methods, Static Methods)
BankAccount Example: Methods in Action
This snippet provides a practical example of using instance, class, and static methods in a BankAccount
class. It demonstrates how these methods can be used to manage bank account operations and calculations.
The BankAccount Class
This example simulates a bank account with features like deposits, withdrawals, and interest calculations. It showcases the use of different method types to handle these operations.
Code Example: BankAccount with Different Method Types
The BankAccount
class has methods for depositing and withdrawing funds (instance methods), setting the interest rate for all accounts (class method), and calculating interest (static method).
class BankAccount:
interest_rate = 0.02 # Class variable for interest rate
accounts = [] #Class variable to hold accounts
def __init__(self, account_number, balance=0):
self.account_number = account_number
self.balance = balance
BankAccount.accounts.append(self)
# Instance method: Deposit money
def deposit(self, amount):
if amount > 0:
self.balance += amount
return f"Deposited {amount}. New balance: {self.balance}"
else:
return "Invalid deposit amount."
# Instance method: Withdraw money
def withdraw(self, amount):
if 0 < amount <= self.balance:
self.balance -= amount
return f"Withdrew {amount}. New balance: {self.balance}"
else:
return "Insufficient funds or invalid withdrawal amount."
# Class method: Change the interest rate for all accounts
@classmethod
def set_interest_rate(cls, rate):
cls.interest_rate = rate
return f"Interest rate set to {rate}"
# Class method: Get all created accounts
@classmethod
def get_all_accounts(cls):
return cls.accounts
# Static method: Calculate interest (helper function)
@staticmethod
def calculate_interest(principal, rate, time):
return principal * rate * time
# Example usage
account1 = BankAccount("12345", 1000)
account2 = BankAccount("67890", 500)
print(account1.deposit(500)) # Output: Deposited 500. New balance: 1500
print(account2.withdraw(200)) # Output: Withdrew 200. New balance: 300
print(BankAccount.set_interest_rate(0.03)) # Output: Interest rate set to 0.03
print(BankAccount.calculate_interest(1000, BankAccount.interest_rate, 1)) # Output: 30.0
accounts = BankAccount.get_all_accounts()
print(f'Number of Accounts created : {len(accounts)}') # Output : Number of Accounts created : 2
Explanation of the Code
__init__
method initializes the account number and balance for a new bank account instance. The new instance is added to a list containing all accounts (class variable).deposit
and withdraw
methods are instance methods that modify the balance of the specific account.set_interest_rate
method is a class method that changes the interest_rate
for all BankAccount
instances.get_all_accounts
method is a class method that returns a list of all the BankAccount
instances created.calculate_interest
method is a static method that calculates the interest earned based on the principal, rate, and time. It doesn't depend on any specific instance or class data.
Real-Life Use Case Expanded
In a real banking system, you would use these methods extensively. deposit
and withdraw
are core functions. The set_interest_rate
method might be used by administrators to update interest rates across the bank. calculate_interest
could be used for reporting and account statement generation. get_all_accounts
could be used for statistics or reporting purposes.
Best Practices
Interview Tip
When discussing this example in an interview, be prepared to explain why each method is the type that it is. Explain how instance methods are tied to individual account instances, class methods affect all accounts, and static methods are simply helper functions related to banking calculations.
When to use them in this Example
Memory footprint Considerations
Alternatives for this Example
calculate_interest
, a function outside the class could be used, if no direct connection to the BankAccount class is needed.
Pros of this design
Cons of this design
FAQ
-
Why is `interest_rate` a class variable?
Theinterest_rate
is a class variable because it's a property that applies to all bank accounts. Changing it affects all instances of theBankAccount
class. -
Could `calculate_interest` be an instance method?
While it could be implemented as an instance method, it's more appropriate as a static method because it doesn't need to access or modify any instance-specific data. It's a purely computational function. -
What happens if I try to withdraw more money than the balance?
Thewithdraw
method checks if the withdrawal amount is valid and if there are sufficient funds. If not, it returns an error message: "Insufficient funds or invalid withdrawal amount."