› Forums › Python › Why Doesn’t `FileHandler(“phonebook.txt”)` Automatically Add Data to the PhoneBook?
- This topic is empty.
-
AuthorPosts
-
December 28, 2025 at 11:31 pm #5902
Context
While working through the Application Development section of the MOOC course, you may encounter the following constructor in a phone book application:
class PhoneBookApplication: def __init__(self): self.__phonebook = PhoneBook() self.__filehandler = FileHandler("phonebook.txt") # add the names and numbers from the file to the phone book for name, numbers in self.__filehandler.load_file().items(): for number in numbers: self.__phonebook.add_number(name, number)A common and very reasonable question is:
Why isn’t this line enough?
self.__filehandler = FileHandler("phonebook.txt")Shouldn’t it automatically add everything from the file into the phonebook?
Let’s clear up this confusion step by step.
Short Answer
Creating a
FileHandlerobject does not automatically read the file or modify the phonebook.It only:
- Stores the filename
- Prepares the object for future file operations
Nothing is added unless you explicitly tell the program to do so.
What Actually Happens When the Program Starts
Step 1: Creating an empty phonebook
self.__phonebook = PhoneBook()- A new
PhoneBookobject is created - It contains no names or numbers
Step 2: Creating the file handler
self.__filehandler = FileHandler("phonebook.txt")This line:
- Creates a
FileHandlerobject - Stores
"phonebook.txt"internally
It does not:
- Read the file
- Parse its contents
- Add anything to the phonebook
At this point:
- The phonebook is empty
- The file handler just knows which file to work with
What
load_file()Really DoesThe
load_file()method is responsible for:- Opening the file
- Reading its contents
- Converting the data into a dictionary
- Returning that dictionary
Example return value:
{ "Alice": ["12345", "67890"], "Bob": ["55555"] }Importantly:
load_file()returns data — it does not store it anywhere else.
Why We Need Extra Code to Connect File and PhoneBook
This part is crucial:
for name, numbers in self.__filehandler.load_file().items(): for number in numbers: self.__phonebook.add_number(name, number)Here’s what this code does:
- Takes raw data from the file
- Loops through each name and number
- Uses
PhoneBook’s public method (add_number) - Stores the data correctly inside the phonebook
Without this loop:
- The file is read
- The data exists
- But nothing is connected
Why Isn’t This Logic Inside
FileHandler?This is intentional and follows good software design principles.
Separation of Responsibilities
Each class has one clear job:
Class Responsibility FileHandlerRead and write files PhoneBookStore and manage contacts PhoneBookApplicationCoordinate between components If
FileHandlerstarted adding data toPhoneBook:- It would need to know how the phonebook works internally
- The code would become tightly coupled
- Reusability and maintainability would suffer
A Simple Real-World Analogy
- FileHandler is a delivery person 📦
- PhoneBook is a storage shelf 📚
- Application is you 👤
The delivery person brings the package.
You decide:- where to open it
- how to organize the contents
- where each item should go
The delivery person does not organize your shelves.
Why Not Read the File Inside the Constructor Automatically?
Putting heavy logic inside a constructor is usually a bad idea because:
- Constructors should only initialize state
- Side effects make testing harder
- Errors during object creation become harder to handle
Good design rule:
Constructors set things up — they don’t do the work.
Key Takeaway
Creating an object does not mean it performs actions automatically.
Objects:
- Hold state
- Expose methods
- Do nothing until those methods are called
The extra code exists because connecting components is the job of the application, not the file handler.
One-Line Summary for Learners
FileHandlerreads data,PhoneBookstores data, andPhoneBookApplicationconnects the two — each class does exactly one job.
-
AuthorPosts
- You must be logged in to reply to this topic.

