How to Use Python Pickle [+Examples]

Download Now: Free Introduction to Python
Marquel Ellis

Published:

If you’re a Python user, then you may have heard of the pickle module. It’s one of Python’s most versatile modules, used for storing objects so they can be retrieved during software development or runtime.

python pickle represented by a pickle

This pickle module is a go-to for converting and storing data. In this post, we’ll cover steps for implementing Python pickle and examples of pickle in action.

Download Now: An Introduction to Python [Free Guide]

What is the pickle module in Python?

Python Pickle. Pickle can serialize (convert into a format that can be stored) and deserialize (convert structure back into its original format) data. With the pickle module, you can convert any valid Python object into a stream of bytes for storage or transmission over a network.

When it comes to serializing (converting data structure into a format that can be stored) and deserializing (converting data structure back into its original format), the pickle module is your go-to tool.

With the pickle module in Python, you can effortlessly convert any valid Python object into a stream of bytes for storage or transmission over a network. You are then able to recreate the same original object from this serialized form with deserialization.

The pickle module allows you to preserve objects such as dictionaries, functions, and custom classes in a serialized format — whether it’s stored in files or transmitted across networks. When the need arises again for whatever was preserved, it can be retrieved from the file so that its original Python object is restored.

The pickle module is a great resource when you want to store and reuse objects. For example, the functions dump() and load() help serialize/deserialize objects. Dumps() and loads() handle that same process for memory instead of external storage.

Put simply: Pickle makes it easy to use data over multiple sessions without losing its integrity.

It's essential to exercise caution when employing the pickle module, particularly with untrustworthy data. A lack of vigilance can lead to running unscrupulous code while deserializing. To avoid this security hazard, we suggest that other serialization formats, such as JSON or XML, be utilized instead.

When to Use the Pickle Module

The pickle module in Python is a valuable asset for saving and transferring intricate data structures. Here are some situations when it can be advantageous to use.

Saving and Loading Machine Learning Models

Training machine learning models takes a significant amount of time. To avoid having to retrain the model in the future, we can use the pickle module to serialize it into a file for easy access later. When needed, this saved model can be retrieved from its saved file for immediate use.

Caching

Training machine learning models takes a significant amount of time. To avoid having to retrain the model in the future, we can use the pickle module to serialize it into a file for easy access later. When needed, this saved model can be retrieved from its saved file for immediate use.

Distributed Computing

When performing distributed computing and sending complex data across machines, the pickle module simplifies the process. Pickle serializes information for transmission over the network.

Storing Application State

In some applications, we may need to store the application state between sessions. The pickle module can be used to serialize the state and save it to a file. Later, the state can be deserialized from the file and used to restore the application to its previous state.

When dealing with untrusted data, take caution when using the pickle module, as arbitrary code can be executed during deserialization. Because of this risk, instead of relying on the pickle module for transmitting data over a network, more secure formats such as JSON or XML are strongly recommended.

To get Python Pickle up and running, we must first import the pickle module. Then, we can utilize the dump() function to write a Python object into a file for future use. You can alternatively employ the load() function to read this object from its file location.

Here is an example of how you might go about creating your own pickled data:

import pickle

# Define a Python object to pickle
my_object = {‘a’: 123, ‘b’: [1, 2, 3], ‘c’: {‘x’: 0, ‘y’: 9}}

# Serialize the object to a file
with open(‘my_object.pickle’, ‘wb’) as f:
pickle.dump(my_object, f)

# De-serialize the object from the file
with open(‘my_object.pickle’, ‘rb’) as f:
loaded_object = pickle.load(f)

# Check that the de-serialized object is the same as the original object
print(my_object == loaded_object) # Output: True

To use pickle in Python, we must first create an object like my_object. Then, using the dump() function and the ‘wb’ parameter on open(), we can serialize this object to a file called my_object.pickle for future usage. This binary write mode ensures that our data is secure and stored correctly for later access.

Then, we use the load() function to decode the object from its file. We specify ‘rb’ in our open() command when opening the document so that it may be read in binary mode.

To conclude, we verify that the deserialized object is identical to the initial object by comparing them using the == operator.

Be aware that the pickle module can bring up security issues if you‘re using an untrusted source. To guarantee your data’s safety, only unpickle what comes from a trustworthy source or make sure it’s something you serialized yourself.

Python Pickle in Action

Here are three examples of how to use Python pickle.

1. Pickling and Unpickling a Simple Object

import pickle

# Pickle a simple object
my_object = {‘name’: ‘Alice’, ‘age’: 30}
with open(‘my_object.pickle’, ‘wb’) as f:
pickle.dump(my_object, f)

# Unpickle the object
with open(‘my_object.pickle’, ‘rb’) as f:
loaded_object = pickle.load(f)

# Print the unpickled object
print(loaded_object) # Output: {‘name’: ‘Alice’, ‘age’: 30}

Let‘s consider a basic dictionary object with information about someone’s name and age. We‘ll save this as ’my_object.pickle' using pickle.dump().

Then we can use load() to restore it into the loaded_object variable, before finally printing out our unpickled object for confirmation — proving that nothing has changed from its original form.

2. Pickling and Unpickling a Complex Object

import pickle

# Pickle a complex object
class Person:
def __init__(self, name, age, email):
self.name = name
self.age = age
self.email = email

def __repr__(self):
return f‘Person(name={self.name}, age={self.age}, email={self.email})’

my_object = [Person(‘Alice’, 30, ‘alice@example.com’),
Person(‘Bob’, 25, ‘bob@example.com’),
Person(‘Charlie’, 35, ‘charlie@example.com’)]

with open(‘my_object.pickle’, ‘wb’) as f:
pickle.dump(my_object, f)

# Unpickle the object
with open(‘my_object.pickle’, ‘rb’) as f:
loaded_object = pickle.load(f)

# Print the unpickled object
print(loaded_object) # Output: [Person(name=Alice, age=30, email=alice@example.com),
# Person(name=Bob, age=25, email=bob@example.com),
# Person(name=Charlie, age=35, email=charlie@example.com)]

In this example, we define a complex object Person that has attributes such as name, age, and email. We create a list of Person objects and pickle it into a file. We then unpickle the object from the file using pickle.load() and store it in the loaded_object variable.

Finally, we print the unpickled object to confirm that it is the same as the original object.

3. Using Pickle to Cache Function Results

import pickle

# A function that takes a long time to compute
def expensive_computation():
# ...
return result

# Check if the result is already cached
try:
with open(‘result.pickle’, ‘rb’) as f:
result = pickle.load(f)
except FileNotFoundError:
# Compute the result and cache it
result = expensive_computation()
with open(‘result.pickle’, ‘wb’) as f:
pickle.dump(result, f)

# Use the cached result
print(result)

With this example, we create a function called expensive_computation() that takes an extended period of time to compute. To optimize the process, we turn to pickle and cache the result of the said function into a file titled ‘result.pickle’.

The next time it is accessed, if there is already cached data present - great! We instantly load from the stored file.

Getting Started

Python Pickle is an incredibly robust library for serializing and deserializing objects. With pickle, we can effortlessly store intricate objects and rapidly restore them afterward. This is perfect for caching pricy computations and storing or recovering information from Python applications.

python

Topics: What Is Python?

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Pop up for DOWNLOAD NOW: FREE INTRODUCTION TO PYTHON FREE INTRODUCTION TO PYTHON

A guide for marketers, developers, and data analysts.

CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

START FREE OR GET A DEMO