Introduction to Pickle

Posted by Afsal on 05-May-2023

Hi Pythonistas!

Today we will learn about a Python standard library called Pickle. Pickle is a Python module that converts a Python object into a byte stream and vice versa. The byte stream can then be stored as a file or transmitted over a network. The byte stream created by Pickle is platform-independent and can be used to reconstruct the original Python object.

Pickling data

import pickle

user1 = {'name': 'John', 'age': 25, 'gender': 'male'}
user2 = {'name': 'Mary', 'age': 30, 'gender': 'female'}

with open('users.pickle', 'wb') as f:
    pickle.dump(user1, f)
    pickle.dump(user2, f)

Output

Once the code is executed a file with the name users.pickle will be created in the current working directory

Loading data from the pickled file

import pickle

with open('users.pickle', 'rb') as f:
    user1 = pickle.load(f)
    user2 = pickle.load(f)

print(user1)
print(user2)

Output

{'name': 'John', 'age': 25, 'gender': 'male'}

{'name': 'Mary', 'age': 30, 'gender': 'female'}

When to use

Pickling can we very useful for caching the state of variable which are very expensive to compute. I will share my personal experience with this pickle module. During my final project in college, we were creating an ML model to find facial expressions. The issue we were facing was initial running of the script was very very slow. Because we trained the model in the initial step and loaded the model(numpy array) into memory once that is done testing the model was fast. So we decided to make a way to save the state in the file and loading from there instead of computing this every time. So I decided to use the pickle module and save the model in the pickle file. So in the initial load script load of the state from the file this was very fast. 

Hope you have learned something from this post. Please share your valuable suggestions with afsals@parseltongue.co.in