Some Notes on Serializing Objects in Python

I was playing with .NET serialization at work the other day and got curious about how Python does it. Serialization is a little confusing in the .NET world, but it's not an insurmountable task to grasp it. For one, there is more than a single implementation of serialization within the .NET base class library, or namely, System.Xml.Serialization and System.Runtime.Serialization, which respectively implement XML and binary serialization. The techniques used in each implementation are also disparate, having the binary serialization make heavy use of class attributes, while the XML implementation uses a method call to XmlSerializer.Serialize.

The Python implementation of serialization is much simpler, concise and easier to understand. It is implemented as a Standard Library module called Pickle. The actions to serialize and deserialize classes are implemented as simple function class and there is no need to put attributes on classes. Let's see how it works.

First import the pickle module and then declare a class called Person as in the code below:

import pickle

class Person(object):
    def __init__(self, first_name=None, last_name=None, age=None):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age

Now create two instances of the Person class above and place them in alist.

p1 = Person('Jane', 'Doe', 26)
p2 = Person('John', 'Hancock', 33)
people = []
people.append(p1)
people.append(p2)

Next serialize the list to a file and then read it back into a new list. First serialize the list:

fname = 'peoplelist.dat'
f1 = open(fname, 'wb')
pickle.dump(people, f1)
f1.close()

Finally, read the contents of the serialized file back into a new list and print out the name and age of each person:

f2 = open(fname)
new_people = pickle.load(f2)
for person in new_people:
    print '%s %s is %d years old.' % (person.first_name, person.last_name, person.age)

That's it... Serialization in Python is just too easy!

Comments

Comments powered by Disqus