How it works...

The load_contacts function is responsible for reading the CSV file and transforming all the records into a list of Contact instances.

Each row read by csv.reader is returned as a tuple of strings, created by splitting the corresponding line using the comma delimiter. Since this tuple uses the same order as the parameters defined in the __init__ method of the Contact class, we can simply unpack it with the * operator. This code can be summarized in a single line using a list comprehension, as follows:

def load_contacts(self):
with open("contacts.csv", encoding="utf-8", newline="") as f:
return [Contact(*r) for r in csv.reader(f)]

There is no problem in returning the list within the with block, since the context manager automatically closes the file when the method execution finishes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset