I am learning Python and in particular learning about classes. Since I'm self teaching, I'd appreciate any feedback and constructive criticism.
I have a couple specific questions:
- In Python you can have multiple functions and classes in a single file (module), but is it considered bad practice to do so?
- Python doesn't have
private
, so is there a point to having accessor methods like I do below? - My IDE suggest I put a comment beneath each function signature. Is it still good to do this when the functions are short and simple, like
withdraw(x)
in a bank account?
BankAccount.py
# bank account class
class BankAccount:
def __init__(self, name, balance=0.00):
self.name = name
self.balance = balance
def deposit(self, amount):
"""make a deposit"""
self.balance += amount
def withdraw(self, amount):
"""make a withdraw"""
if amount > self.balance:
raise ValueError("insufficient funds")
self.balance -= amount
def get_balance(self): #are accessor methods needed in Python?
"""check the balance"""
return self.balance
def main():
customer1 = BankAccount('Alex')
print(customer1.get_balance())
customer1.deposit(100)
customer1.withdraw(30)
print(customer1.get_balance())
customer2 = BankAccount('Sam', 200)
print(customer2.get_balance())
if __name__ == "__main__":
main()
bank account class
comment, as it doesn’t seem to add any insight to the codeclass BankAccount
. \$\endgroup\$