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.

serialize.py

serialize.py
import pickle
from datetime import date


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


emp1 = Employee("Chandra Shekhar", date(2020, 2, 6), "SDE")
emp2 = Employee("John", date.today(), "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 serialze.py

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.

deserialize.py

deserialize.py
import pickle

from serialize import Employee

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

for employee in employees:
    print(type(employee))
    print(employee.__dict__)

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

Output:

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

References:

Happy Learning 🙂

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment