When you have just a few classes or constructor functions and their prototypes, hundreds of lines of object-oriented code are easy to organize and maintain. However, as the number of object-oriented blueprints start to increase, it is necessary to follow some rules to organize the code and make it easy to maintain.
The first and the most straightforward way. A Class Object from one Python file can be imported to another one for usage.
Python File Account.py
# Class Test Account
class Account:
def __init__(self, number=0.0, balance=0.0, account_type=""):
print("Account Created")
self.number = number
self.balance = balance
self.account_type = account_type
def get_number(self):
return self.number
def get_balance(self):
return self.balance
def get_type(self):
return self.account_type
def to_string(self): # Display the information as String
print(f"Account[Number = {str(self.number)}, Balance = {str(self.balance)}, "
f"AccountType = {self.account_type}]")
def credit(self, amount): # Add amount to balance
self.balance = self.balance + amount
def debit(self, amount): # Subtract amount to balance
if self.balance < amount:
raise ValueError("Cannot subtract the balance to the given amount, "
"or Amount Exceeded")
self.balance = self.balance - amount
def transfer_to(self, amount, to_account):
if self.balance < amount:
raise ValueError("Cannot transfer the given amount from the balance, "
"or Amount Exceeded")
# Transfer from Account to Account
self.balance = self.balance - amount
to_account.balance = to_account.balance + amount
Import Class Account to another python file: TestAccount.py
# Import Account
from Account import Account
def main():
balance1 = float(input("Enter balance for Account 1: "))
balance2 = float(input("Enter balance for Account 2: "))
account1 = Account(11, balance1, "USD")
account2 = Account(12, balance2, "USD")
# Add 100$ to account1, and 50$ to account2
account1.credit(100)
account2.debit(50)
account1.transfer_to(70, account2)
print("=================Account1=================")
print(f"+ Number: {account1.get_number()}")
print(f"+ Balance: {account1.get_balance()}")
print(f"+ Account Type: {account1.get_type()}")
print("==========================================")
print()
print("=================Account2=================")
print(f"+ Number: {account2.get_number()}")
print(f"+ Balance: {account2.get_balance()}")
print(f"+ Account Type: {account2.get_type()}")
print("==========================================")
if __name__ == '__main__':
main()
Each programming language provides different elements and resources to generate object-oriented code. In addition, each programming language provides its own mechanisms that allow you to organize and group different object-oriented elements. Thus, it is necessary to define rules for each of the programming languages.
Imagine that we have to create and furnish house floor plans with a drawing software that allows you to load objects from files.
We have a huge amount of objects to compose our floor plan, such as entry doors, interior doors, square rooms, interior walls, windows, spiral stairs, straight stairs, and kitchen islands.
If we use a single folder in our file system to save all the object files, it will take us a huge amount of time to select the desired object each time we have to add an object to our floor plan.
Organize our objects in the following five folders: