Python Object Serialization
Python object serialization is the process of converting a Python object into a format that can be stored or transmitted and later reconstructed. This is useful for saving data to files, sending objects over a network, or storing structured data in databases.
Why Use Object Serialization?
Serialization is essential when working with complex objects that need to be saved and restored across different sessions or transferred between different systems. In Python, the pickle
module provides an easy way to serialize and deserialize objects.
Modules for Serialization
Module | Description |
---|---|
pickle | Serializes and deserializes Python objects into a binary format. |
json | Used for serializing Python objects into JSON format (text-based). |
shelve | Persists Python objects in a dictionary-like structure. |
Serialization Using pickle
The pickle
module allows you to serialize Python objects into binary format, which can be stored in a file or sent over a network.
1. Serializing and Deserializing a Python Object
In this example, we will serialize a Python dictionary, store it in a file, and then deserialize it back into a Python object.
We use the pickle.dump()
function to serialize the dictionary and save it to a file. Later, we use pickle.load()
to read the file and deserialize the object.
import pickle
# Define a dictionary to serialize
data = {"name": "Arjun", "age": 25, "city": "New York"}
# Serialize and save to file
with open("data.pkl", "wb") as file:
pickle.dump(data, file)
# Deserialize and load from file
with open("data.pkl", "rb") as file:
loaded_data = pickle.load(file)
# Print the loaded data
print("Deserialized Data:", loaded_data)
Output:
Deserialized Data: {'name': 'Arjun', 'age': 25, 'city': 'New York'}
The object is successfully serialized and written to a file. Later, it is retrieved and deserialized into the original dictionary.
2. Serializing a Custom Python Object
We can serialize an instance of a user-defined class. The class must be defined in the script where the object is deserialized.
Here, we create a Person
class, serialize its instance, and later deserialize it.
import pickle
# Define a simple class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return f"Person(name={self.name}, age={self.age})"
# Create an instance of the class
person = Person("Ram", 25)
# Serialize and save the object
with open("person.pkl", "wb") as file:
pickle.dump(person, file)
# Deserialize and load the object
with open("person.pkl", "rb") as file:
loaded_person = pickle.load(file)
# Print the deserialized object
print("Deserialized Object:", loaded_person)
Output:
Deserialized Object: Person(name=Ram, age=25)
The instance is successfully serialized and later reconstructed as the same object.
3. Handling Serialization Errors
Not all objects can be pickled. Objects containing file handles, sockets, or other system resources cannot be serialized.
In this example, we attempt to pickle an open file handle, which leads to an error.
import pickle
try:
file = open("example.txt", "w")
# Attempt to serialize an open file object
with open("file.pkl", "wb") as f:
pickle.dump(file, f)
except pickle.PicklingError as e:
print("Pickling Error:", e)
finally:
file.close() # Always close the file
Output:
Pickling Error: can't pickle '_io.TextIOWrapper' object
Since an open file handle cannot be pickled, an error is raised. Always ensure objects are serializable before attempting to pickle them.
Using JSON for Serialization
The json
module provides a text-based serialization format that is human-readable and widely used for data exchange.
1. Serializing a Dictionary with JSON
Unlike pickle
, JSON only supports basic data types (strings, numbers, lists, and dictionaries). Here, we serialize a dictionary into JSON format.
import json
# Define a dictionary
data = {"name": "Arjun", "age": 25, "city": "New York"}
# Serialize to JSON and write to a file
with open("data.json", "w") as file:
json.dump(data, file)
# Deserialize from JSON file
with open("data.json", "r") as file:
loaded_data = json.load(file)
print("Deserialized JSON Data:", loaded_data)
Output:
Deserialized JSON Data: {'name': 'Arjun', 'age': 25, 'city': 'New York'}
The dictionary is successfully stored as a JSON file and later restored into its original format.