In this tutorial, we are going to see what is python object serialization with Pickle and how to work with that.

Python Serialization:

By definition, serialization is a concept of storing the state of an object it may be in a secondary storage device, this might be for future use or while transmitting the objects from one machine to another over the network.

Sometimes we may need to stop the running program and restart again it where it was left off with the same state. Actually, there are many ways to do it but one way is by using serialization.

Here the process of storing the state of an object is called serialization, whereas the process of retrieving the same state where it was left off is called de-serialization.

Python Pickle:

Python pickle is one of the widely used objects serialization/deserialization modules. Pickle has its own terminology for this serialization. It says the process of converting Python object hierarchy into byte stream is called pickling whereas unpickling is reverse.

Object Serialization with Pickle Example:

Pickle comes along with Python installation, hence no need to install explicitly.

Let’s create an employee class and have some properties init. Try to serialize and deserialize the data.
import pickle
from datetime import date

class Employee:
    def __init__(self, name: str, joining_date: str, designation: str) -> None: = name
        self.joining_date = joining_date
        self.designation = designation

emp1 = Employee("Chandra Shekhar", date(2020, 2, 6), "SDE")
emp2 = Employee("John",, "RM")
emp3 = Employee("Peter", date(2019, 12, 16), "Architect")

employees = [emp1, emp2, emp3]

with open("employees.pickle", "wb") as f:
    pickle.dump(employees, f)

In the above example, we have created the Employee class having 3 fields.

Writing the employee’s objects into employees.pickle file (.pickle is the extension of pickle file) using write bytes. The pickle.dump() function is used to write the pickled representation of objects into a file.

Let’s run the above program:

(venv) %python

If everything went well you have to see employees.pickle file in the same directory, it has the binary representation of the employee’s list.

Now let’s deserialize (unpickling) it.
import pickle

from serialize import Employee

with open("employees.pickle", "rb") as f:
    employees = pickle.load(f)

for employee in employees:

In the above example, I just try to read the employees.pickle file using read bytes and unpickling the data usingpickle.load() function.


(venv) % python
<class 'serialize.Employee'>
{'name': 'Chandra Shekhar', 'joining_date':, 2, 6), 'designation': 'SDE'}
<class 'serialize.Employee'>
{'name': 'John', 'joining_date':, 7, 8), 'designation': 'RM'}
<class 'serialize.Employee'>
{'name': 'Peter', 'joining_date':, 12, 16), 'designation': 'Architect'}


Happy Learning 🙂

Leave A Comment