› Forums › Python › Why We Use get_numbers() Instead of Accessing the Dictionary Directly (Python OOP)
- This topic is empty.
-
AuthorPosts
-
January 23, 2026 at 9:42 pm #5960
π Context (Initial Code)
We have a
PhoneBookclass that stores names and phone numbers, and aPhoneBookApplicationclass that provides a text-based user interface (UI).β PhoneBook (Core Logic)
class PhoneBook: def __init__(self): self.__persons = {} def add_number(self, name: str, number: str): if not name in self.__persons: self.__persons[name] = [] self.__persons[name].append(number) def get_numbers(self, name: str): if not name in self.__persons: return None return self.__persons[name]β PhoneBookApplication (User Interface)
class PhoneBookApplication: def __init__(self): self.__phonebook = PhoneBook() def search(self): name = input("name: ") numbers = self.__phonebook.get_numbers(name) if numbers == None: print("number unknown") return for number in numbers: print(number)
β Q&A: Why Not Access
__persons[name]Directly insearch()?β Q: Why donβt we write this inside
search()?numbers = self.__phonebook.persons[name]β Answer:
Becausepersonsis not a public attribute, and the dictionary is actually stored as:self.__personsinside the
PhoneBookclass, which is private due to the double underscore__.So the UI class (
PhoneBookApplication) cannot (and should not) directly access it.
β Q: What happens if we try to access the dictionary directly anyway?
β Answer:
If you try something like this:numbers = self.__phonebook.__persons[name]It will fail because
__personsis private, and Python will raise an error like:β AttributeError (cannot access private attribute)
Also, direct access like this would crash if the name doesnβt exist, causing a:
β KeyError
β Q: Why is using
get_numbers(name)better?β Answer:
Becauseget_numbers()is a public method designed to:- safely check if the name exists
- return the numbers if found
- return
Noneif not found
Thatβs why the UI can handle missing names properly:
if numbers == None: print("number unknown")No crash β
β Q: What programming principle does this follow?
β Answer:
This is called Encapsulation (and also supports Separation of Concerns).It means:
PhoneBookis responsible for storing and managing dataPhoneBookApplicationis responsible only for interacting with the user
So the UI should use methods like:
β
add_number()
βget_numbers()instead of touching internal variables directly.
β Q: What is the biggest advantage of doing it this way?
β Answer:
Your program becomes easy to maintain and improve.For example, if later you change how data is stored (dictionary β database), you only update
PhoneBook.The UI code still works, because it only relies on:
self.__phonebook.get_numbers(name)
β Final Takeaway
We donβt access
persons[name]directly in the UI because the phonebook data is private and should be managed only through public methods likeget_numbers(), which also prevents errors and keeps the design clean. -
AuthorPosts
- You must be logged in to reply to this topic.

