Python > Working with Data > File Formats > Pickle and Serialization (`pickle` module)
Pickling a Custom Class
This snippet demonstrates how to use the pickle
module to serialize an instance of a custom Python class. This is useful when you need to save the state of an object, including its attributes, and restore it later.
Importing the Pickle Module
First, you need to import the pickle
module to use its functions for serialization and deserialization.
import pickle
Defining a Custom Class
We define a custom class called Person
with a constructor and a greet
method.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name} and I am {self.age} years old."
Creating an Instance of the Class
We create an instance of the Person
class.
person = Person('Bob', 40)
Pickling and Saving the Object to a File
This section shows how to pickle the Person
object and save it to a file. We open the file in binary write mode ('wb') and use pickle.dump()
to write the serialized object to the file.
filename = 'person.pkl'
with open(filename, 'wb') as file:
pickle.dump(person, file)
Unpickling and Loading the Object from a File
This section demonstrates how to read the pickled data from the file and deserialize it back into a Person
object. We open the file in binary read mode ('rb') and use pickle.load()
to read the serialized object from the file. We then call the greet
method to verify that the object has been successfully restored.
with open(filename, 'rb') as file:
loaded_person = pickle.load(file)
print(loaded_person.greet())
Concepts Behind the Snippet
Pickling allows you to preserve the state of a Python object, including the values of its attributes. This is particularly useful for saving and restoring complex data structures or application states. When pickling a custom class, the pickle
module handles the serialization of the object's attributes automatically.
Real-Life Use Case
Pickling custom classes is commonly used in game development to save the player's progress, in machine learning to save trained models, and in scientific computing to save simulation results.
Best Practices
Person
class, you might not be able to unpickle older Person
objects. Consider implementing versioning mechanisms to handle such scenarios.
Interview Tip
Explain the process of pickling a custom class, including how the pickle
module handles the serialization of the object's attributes. Also, be prepared to discuss potential issues like versioning and security.
When to Use Them
Use pickling with custom classes when you need to persist the state of an object, including its attributes and any other relevant data. This is useful for saving application states, game progress, or trained machine learning models.
Memory footprint
The memory footprint of pickling depends on the size and complexity of the object being serialized. Larger and more complex objects will result in larger pickled files. Keep this in mind when dealing with very large datasets or objects.
Alternatives
While JSON and YAML can be used to serialize simple class instances by manually extracting the data from the object and representing it in those formats, pickling provides a more seamless and automated way to serialize arbitrary class instances including complex ones. Other alternatives might include creating custom serialization methods if you need specific control over the process.
Pros
Cons
FAQ
-
What happens if I change the class definition after pickling an object?
If you significantly change the class definition, you might not be able to unpickle older objects. In some cases, you can implement compatibility mechanisms to handle changes, but it's generally best to avoid significant changes to class definitions when dealing with pickled objects. -
Can I pickle objects that contain other objects as attributes?
Yes, pickling can handle objects that contain other objects as attributes. Thepickle
module recursively serializes all objects in the object graph.