- This topic is empty.
-
AuthorPosts
-
March 9, 2026 at 3:13 pm #6186
πΉ Initial Context
In the Python PhoneBook application, we store people and their information in a dictionary inside the
PhoneBookclass.Here is the relevant code:
class PhoneBook: def __init__(self): self.__persons = {} def add_number(self, name: str, number: str): if name not in self.__persons: self.__persons[name] = Person(name) self.__persons[name].add_number(number) def add_address(self, name: str, address: str): if name not in self.__persons: self.__persons[name] = Person(name) self.__persons[name].add_address(address) def get_entry(self, name: str): return self.__persons.get(name)Many learners notice the last line and ask:
Where does the
get()function come from?
What exactly doesself.__persons.get(name)return?Letβs clarify.
πΉ What Is
get()?The
get()function is a built-in method of Python dictionaries.In our program:
self.__persons = {}means
__personsis a dictionary. Python dictionaries automatically have several built-in methods such as:get()keys()values()items()update()pop()
So when we write:
self.__persons.get(name)we are calling the dictionary method
get().
πΉ What Does
dict.get()Do?The general syntax is:
dictionary.get(key)It means:
βReturn the value stored for this key.β
If the key does not exist,
get()returns:Noneinstead of raising an error.
πΉ Example Without Using
get()Consider this dictionary:
persons = {"Alice": "12345"}If we try:
print(persons["Bob"])Python raises an error:
KeyError: 'Bob'Because
"Bob"is not a key in the dictionary.
πΉ Example Using
get()Using
get()avoids this error:print(persons.get("Bob"))Output:
NoneThe program continues safely.
πΉ How It Works in the PhoneBook
Inside the
PhoneBookclass, the dictionary stores Person objects.Example internal structure:
{ "Eric" β Person object "Emily" β Person object }When we call:
phonebook.get_entry("Eric")Python executes:
self.__persons.get("Eric")Result:
Person object for Eric
πΉ What If the Name Is Not Found?
Example:
phonebook.get_entry("Alice")Python runs:
self.__persons.get("Alice")Since
"Alice"does not exist in the dictionary, the result is:NoneThis prevents the program from crashing.
πΉ Visual Explanation
Dictionary contents:
"Eric" β Person("Eric") "Emily" β Person("Emily")Calling:
get_entry("Eric")returns:
Person("Eric")Calling:
get_entry("Alice")returns:
None
πΉ Important Detail
The
get_entry()method returns the value stored in the dictionary, not the key.In this program:
name β Person objectSo the returned value is the Person object.
Example:
person = phonebook.get_entry("Eric") print(person.numbers()) print(person.address())The UI can now access Eric’s data through the returned object.
πΉ Why
get()Is a Good Design ChoiceUsing
get()is safer than:self.__persons[name]because
get():β avoids
KeyError
β allows easy checking for missing entries
β keeps the program stable
β Final Takeaway
The line:
return self.__persons.get(name)means:
βLook up the name in the dictionary and return the associated value.β
In the phone book program, this value is a Person object.
If the name is not found, Python returns
Noneinstead of crashing.Understanding
get()is important because it is widely used when working with dictionaries in Python.
-
AuthorPosts
- You must be logged in to reply to this topic.
