Home > python > pickle


The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy.” It’s a.k.a. serialization. (source)

For writing this entry, I also used this post on PyMOTW.

By default, the pickled byte stream contains ASCII characters only. But it’s fine, it makes debugging easier.

The cpickle module is a C implementation, which is a lot faster than the pure Python pickle module.

The pickle format is specific to Python, so you can use it only between two Python programs.

Warning! The pickle module is not intended to be secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source.

#!/usr/bin/env python

    import cPickle as pickle
    import pickle

def main():
    data1 = [ { 'a':'one', 'b':2, 'c':3.0 } ]
    print 'DATA: ',

    data1_string = pickle.dumps(data1)    # here: pickling
    print 'PICKLE:', data1_string

    data2 = pickle.loads(data1_string)    # here: unpickling
    print 'UNPICKLED:',

    print 'SAME?:', (data1 is data2)
    print 'EQUAL?:', (data1 == data2)


if __name__ == "__main__":


DATA:  [{'a': 'one', 'c': 3.0, 'b': 2}]
PICKLE: (lp1
UNPICKLED: [{'a': 'one', 'c': 3.0, 'b': 2}]
SAME?: False
EQUAL?: True

When working with your own classes, you must ensure that the class being pickled appears in the namespace of the process reading the pickle. Only the data for the instance is pickled, not the class definition. The class name is used to find the constructor to create the new object when unpickling.” (source)

That is, when you want to unpickle instances of a class, don’t forget to import the definition of this class!

Categories: python Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: