Python Saving Objects with Pickle
Python’s pickle
module is a tool for serializing and deserializing Python objects.
Serialization is the process of converting Python objects into a byte stream, which can be saved to a file or transmitted over a network.
Deserialization is the reverse process, where you reconstruct Python objects from a byte stream.
What is Pickle?
Pickle is a Python module that allows you to serialize and deserialize objects. Serialization is essential when you want to persist a data or send it across a network.
For serializing basic Python classes, you can use JSON, but for more complex Python objects, you need to use the Pickle module.
Getting Started with Pickle
Import the pickle
module
First, you need to import the pickle
moudle in your Python script:
import pickle
Basic Usage Examples
# Create a Python object (a dictionary)
data = {'name': 'Alice', 'age': 30, 'city': 'New York'}
# Serialize the object to a binary format
with open('data.pkl', 'wb') as file:
pickle.dump(data, file)
In this example, we create a dictionary data
and serialize it to a binary file named data.pkl
.
# Deserialize the object from the binary file
with open('data.pkl', 'rb') as file:
loaded_data = pickle.load(file)
print(loaded_data)
Custom Objects and Pickle
Pickle is not limited to built-in types; it can also handle custom Python objects. To do this, you’ll need to implement the __reduce__
method for your custom classes.
Pickling Custom Python Objects
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
# Create an instance
person = Person('Bob', 25)
# Pickle the object
with open('person.pkl', 'wb') as file:
pickle.dump(person, file)
# Unpickling the object
with open('person.pkl', 'rb') as file:
loaded_person = pickle.load(file)
By defining the __reduce__
method in your custom class, you can control how instances of the class are serialized and deserialized.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __reduce__(self):
return (self.__class__, (self.name, self.age))
In this example, we’ve implemented __reduce__
to specify how to recreate a Person
object during unpicking.
Advanced Pickle Features
Protocol Versions and Compatibility
Pickle supports multiple protocol versions. When picking data, you can specify the protocol version, which affects compatibility with different Python versions.
# Pickling with protocol version 4 (Python 3.4+)
with open('data.pkl', 'wb') as file:
pickle.dump(data, file, protocol=4)