Python Data Persistence - Pickle Module

Neha Kumawat

a year ago

Python’s terminology for serialization and deserialization is pickling and unpickling respectively. The pickle module in Python library, uses very Python specific data format. Hence, non-Python applications may not be able to deserialize pickled data properly. It is also advised not to unpickle data from un-authenticated source.
The serialized (pickled) data can be stored in a byte string or a binary file. This module defines dumps() and loads() functions to pickle and unpickle data using byte string. For file based process, the module has dump() and load() function.
Python’s pickle protocols are the conventions used in constructing and deconstructing Python objects to/from binary data. Currently, pickle module defines 5 different protocols as listed below −
Protocol version 0
Original “human-readable” protocol backwards compatible with earlier versions.
Protocol version 1
Old binary format also compatible with earlier versions of Python.
Protocol version 2
Introduced in Python 2.3 provides efficient pickling of new-style classes.
Protocol version 3
Added in Python 3.0. recommended when compatibility with other Python 3 versions is required.
Protocol version 4
was added in Python 3.4. It adds support for very large objects


The pickle module consists of dumps() function that returns a string representation of pickled data.

from pickle import dump
dct={"name":"Ravi", "age":23, "Gender":"M","marks":75}
print (dctstring)




Use loads() function, to unpickle the string and obtain original dictionary object.

from pickle import load
print (dct)


{'name': 'Ravi', 'age': 23, 'Gender': 'M', 'marks': 75}
Pickled objects can also be persistently stored in a disk file, using dump() function and retrieved using load() function.

import pickle
dct={"name":"Ravi", "age":23, "Gender":"M","marks":75}

#to read
import pickle
print (d)
The pickle module also provides, object oriented API for serialization mechanism in the form of Pickler and Unpickler classes.
As mentioned above, just as built-in objects in Python, objects of user defined classes can also be persistently serialized in disk file. In following program, we define a User class with name and mobile number as its instance attributes. In addition to the __init__() constructor, the class overrides __str__() method that returns a string representation of its object.

class User:
   def __init__(self,name, mob):
   def __str__(self):
return ('Name: {} mobile: {} '. format(,
To pickle object of above class in a file we use pickler class and its dump()method.

from pickle import Pickler
user1=User('Rajani', '', '1234567890')
Conversely, Unpickler class has load() method to retrieve serialized object as follows −

from pickle import Unpickler
print (user1)

Submit Review

We're Online!

Chat now for any query