Splitting Code into Classes and Modules
Hands-on: Classes in a Budget Book Program
A class is a bundle of functions + data grouped by role. As an analogy:
TransactionRepository= the budget book itself (in charge of adding/finding/editing/deleting records)CategoryStore= the person in charge of categoriesBudgetStore= the person in charge of budgets
When each one handles only its own job, the code stays clean.
Requirement: use at least 2 classes
Splitting into Modules
A module = one code file. The idea is to split code by role instead of cramming everything into one file.
budget_app/
├── __init__.py ← marks this folder as a Python package
├── __main__.py ← the entry point when run with python -m budget_app
├── cli.py ← the part that talks to the screen (takes input and prints output)
├── models.py ← defines data boxes such as Transaction
├── storage.py ← the part that reads and writes files (storage layer)
└── services.py ← the actual logic (search, summaries, budget checks, etc.)The idea of layers: the part facing the user (CLI) → the part that makes decisions (Service) → the part that stores data (Storage) → the shape of the data (Model). If calls only go from top to bottom, the code is easier to fix later.
Requirement: split into at least 3 modules
Summary
| Concept | Summary |
|---|---|
| Classes/modules | People in charge of each role + filing boxes for each file |
Source: Codyssey-B1/B2-1
