What is Pickle in python ?

Hi there fellas. In this post i am going to tell you about pickle. It is used for serializing and de-serializing a Python object structure. Any object in python can be pickled so that it can be saved on disk. What pickle does is that it “serialises” the object first before writing it to file. Pickling is a way to convert a python object (list, dict, etc.) into a character stream. The idea is that this character stream contains all the information necessary to reconstruct the object in another python script.

So lets continue:

1. First of all you have to import it through this command:

import pickle

pickle has two main methods. The first one is dump, which dumps an object to a file object and the second one is load, which loads an object from a file object.

2. Prepare something to pickle:
Now you just have to get some data which you can pickle. For the sake of simplicity i will be pickling a python list. So just read the below code and you will be able to figure it out yourself.

import pickle

a = ['test value','test value 2','test value 3']
['test value','test value 2','test value 3']

file_Name = "testfile"
# open the file for writing
fileObject = open(file_Name,'wb') 

# this writes the object a to the
# file named 'testfile'

# here we close the fileObject
# we open the file for reading
fileObject = open(file_Name,'r')  
# load the object from the file into var b
b = pickle.load(fileObject)  
['test value','test value 2','test value 3']

The above code is self explanatory. It shows you how to import the pickled object and assign it to a variable. So now we need to know where we should use pickling. It is very useful when you want to dump some object while coding in the python shell. So after dumping whenever you restart the python shell you can import the pickled object and deserialize it. But there are other use cases as well which i found on stackoverflow. Let me list them below.

1) saving a program's state data to disk so that it can carry on where it left off when restarted (persistence)

2) sending python data over a TCP connection in a multi-core or distributed system (marshalling)

3) storing python objects in a database

4) converting an arbitrary python object to a string so that it can be used as a dictionary key (e.g. for caching & memoization).

One thing to note is that there is a brother of pickle as well with the name of cpickle. As the name suggests it is written in c which makes it 1000 times more faster than pickle. So why should we ever use pickle instead of cpickle ? Here’s the reason

>> Because pickle handles unicode objects.
>> Because pickle is written in pure Python, it's easier to debug.

For further reading i suggest the official pickle documentation or if you want to read more tutorials then check out the sqlite tutorial. Now we come to the end of today’s post. I hope you liked it. Do follow my blog to get regular updates. If you have any suggestions or comments then post them below.


22 thoughts on “What is Pickle in python ?

  1. Pingback: Storing and Loading Data with JSON | Bite Sized Python Tips

    • Hey there! Sorry for not correcting it before. I have corrected it now. I am not sure how that typo slipped through. Thank you very much for letting me know.🙂

  2. Pingback: Setting environment variables with openshift programmatically - BlogoSfera

  3. This helped me understand pickle in Python a tad bit more, but I would have liked to see a real-world example to understand it is used (as where you mentioned storing a state).

  4. Yasoob, nice work! I ran your example on python3 and wanted to point out to your readers a minor change for python3 compatibility. Python3 requires an encoding parameter to the open method for reading binary files. The line “b = pickle.load(fileObject)” will raise an exception unless “fileObject = open(file_Name,’r’)” is changed to “fileObject = open(file_Name,’rb’).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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