Open In App

Difference Between Pickling and Unpickling in Python

Last Updated : 21 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore the key differences between pickling and unpickling in Python. We will discuss the concepts of Python pickling and unpickling in detail, including their purposes, syntax, usage, and considerations for safe and secure pickling and unpickling operations. Let’s dive into the world of pickling and unpickling in Python.

Python Pickling

Pickling is the Python term for serializing an object, which entails transforming it into a binary representation that can be stored in a file or communicated over a network. Python has built-in functions for the pickling objects in the pickle module.

Example: Python Object Serialization

In this example, we are creating a file named ‘person.pickle’ that stores the serialized form of a Python object. We will create a dictionary object ‘person’ which will be serialized. The file object represents the file that will be used for writing the pickled object. The pickle.dump() function is then used to pickle the person object to the file. It takes two arguments – the object to be pickled and the file object to which the pickled object should be written.

Python3




import pickle
 
# Define a Python object
person = {
    "name": "Alice",
    "age": 30,
    "gender": "female"
}
 
# Pickle the object to a binary file
with open("person.pickle", "wb") as file:
    pickle.dump(person, file)
 
print("Pickling completed")


Output:

After running this code, a binary file named ‘person.pickle’ will be created in the same directory, containing the pickled binary representation of the person object.

Pickling completed

Unpickling in Python

In Python, deserializing a pickled object entails turning it from its binary representation back to a Python object that can be used in code. This process is known as unpickling. Python’s built-in pickle module has functions for unpickling objects.

Example: Python Object Deserializing

In this example, we will load the pickle file in our Python code using the load() function of the pickle module. The pickle.load() function is used to deserialize and unpickle the object from the file. It takes one argument – the file object from which the object should be loaded. The unpickled object is stored in the variable data.

Python3




import pickle
 
# load the data from a file
with open('data.pkl', 'rb') as f:
    data = pickle.load(f)
 
# print the data
print(data)


Output

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

Difference Between Pickling and Unpickling

The difference between Pickling and Unpickling is as follows:

Pickling Unpickling

Serialization process that converts a Python object into a binary representation.

Deserialization process that converts a binary representation back into a Python object.

Allows objects to be stored in a file or transmitted over a network.

Retrieves objects from a stored file or received data.

Uses the pickle.dump() function to write objects to a file or a stream.

Uses the pickle.load() function to read objects from a file or a stream.

Objects are converted into a binary format that is not human-readable.

The binary format is converted back into Python objects, which can be used in Python code.

Supports serialization of complex objects, including custom classes, functions, and data structures.

Supports deserialization of complex objects, including custom classes, functions, and data structures.

Can handle circular references and maintain object references during serialization.

Can restore object references during deserialization, preserving the original object relationships.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads