Python Pickle Module

In python, object serialization and deserialization can achieve by using the python pickle module. The python pickle module helps serialize and deserialize the python object's structure into a byte stream format.

 

Generally, serialization is a process of converting the object into a byte stream, and deserialization is an inverse operation, whereas the byte stream will convert back into an object. If you use the python pickle module to implement the serialization process, we will call it pickling, and the deserialization process is called unpickling.

 

By using the python pickle module, you can also serialize the complex custom objects. The objects you serialize using the python pickle module are not human-readable, and the data format used by the pickle is python specific. So, the non-python programs may not be able to reconstruct the python pickled objects.

Pickle Module Functions

The pickle module provides the following functions to implement the pickling (serialization) and unpickling (deserialization) process in python.

 

pickle.dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None)
pickle.dumps(obj, protocol=None, *, fix_imports=True, buffer_callback=None)
pickle.load(file, *, fix_imports=True, encoding="ASCII", errors="strict", buffers=None)
pickle.loads(data, /, *, fix_imports=True, encoding="ASCII", errors="strict", buffers=None)

The pickle module dump()/dumps() functions are useful to serialize the object structure and the load()/loads() functions are useful deserialize the functions.

 

The difference between the pickle dump() and dumps() method is the dump() method will write the serialized/pickled representation of the object to the file, whereas the dumps() method will return the serialized/pickled representation of the object.

 

Similarly, the load() method is useful for reading the pickled representation of an object from the file and returning the reconstituted object. The loads() function will return the reconstituted object from the pickled representation of an object.

Python Pickling and Unpickling Example

Following is the example of serializing/pickling and deserializing/unpickling the object using python pickle dumps() and loads() methods.

 

import pickle

class sampleclass:
    id = 1
    msg = "Welcome to Tutlane"
    objlist = [1, 3, 5]
    objdict = {"Name":"Suresh","Location":"Guntur"}

samp_obj = sampleclass()

#Pickling the object
pickle_obj = pickle.dumps(samp_obj)
print("Pickled Object:", pickle_obj)

#Unpickling the object
unpickle_obj = pickle.loads(pickle_obj)
print("Unpickled Object:", unpickle_obj.objdict)

If you observe the above example, we used pickle dumps() method to serialize/pickle the complete class object (samp_obj) and we used pickle loads() method to deserialize/unpickle the serialized/pickled object(pickle_obj).

 

When you execute the above example, it will return the result as shown below.

 

Pickled Object: b'\x80\x04\x95\x1f\x00\x00\x00\x00\x00\x00\x00\x8c\x08__main__\x94\x8c\x0bsampleclass\x94\x93\x94)\x81\x94.'<>
Unpickled Object: {'Name': 'Suresh', 'Location': 'Guntur'}

If you want to store the serialized object in a file, you need to use binary mode while pickling and unpickling as shown below.

 

import pickle

class sampleclass:
    id = 1
    msg = "Welcome to Tutlane"
    objlist = [1, 3, 5]
    objdict = {"Name":"Suresh","Location":"Guntur"}

samp_obj = sampleclass()

#Pickling the object
file = open('pickleexample.py', 'ab')
pickle.dump(samp_obj, file)
file.close()

#Unpickling the object
file = open('pickleexample.py', 'rb')
unpickle_obj = pickle.load(file)
print("Unpickled Object:", unpickle_obj.objdict)

When you execute the above example, it will return the result as shown below.

 

Unpickled Object: {'Name': 'Suresh', 'Location': 'Guntur'}

Like the pickle module, python has different modules like marshal and json to serialize and deserialize the object structure. In the next chapters, we will learn more about these modules.