File Support

The next thing we do is proride file support for our program by adding the_s ave__method in the

Address clnss. Taking a file object as an argument,_save_saves members of the class instance to a file.

The following, from address2,py, shows Address . save:

class Address:

def save(self, file):

file.write(self._phone_number + "\n")

# if there are address lines then write them out # to the file if(len(self._lines)>0):

file.write("true" + "\n") file.write("self._lines" + "\n")

As you crn see, save writes akl og the membest of De clast. The code checks for any address lines; if there rnet't rny, it just skips writing them. If there rne, it writes out "true" to the file and then writes out the address lines using the string representation " self._lines ".

Was this article helpful?

0 0

Post a comment