Persisting Objects with pickle

I've got good news for you. There's a way to write Python programs that persist Python objects that's much easier than the way we've just done it. It's called the pickle module.

Let's redefine our Address class and take away its ability to write itself to a file. Then we'll use it to demonstrate pickle. This example is from address4.Address.

 class Address: """This class represents an entry in an address book""" #Constructor def __init__(self, name="", phone_number="", address_lines=[]): """The constructor takes three arguments: name to hold the persons name (string) phone_number to hold the persons phone number (string) address_lines to hold the address of the person (list) """ # Assign the name, phone_number and ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.