13
Lesson 13
Pickling and unpickling objects
Objective
By the end of this lesson, students will understand how to serialize and deserialize Python objects using the pickle module. They will learn the concepts of pickling and unpickling, allowing them to save complex data structures to files and retrieve them later.
1. Introduction to pickling:
Pickling is the process of converting a Python object (such as a list, dictionary, or custom class instance) into a byte stream that can be saved to a file or transmitted over a network. This is useful for persisting Python objects across program executions or sending them between different systems.
2. The pickle module:
The pickle module in Python provides a simple way to serialize and deserialize Python objects. It includes methods like pickle.dump() for writing to files and pickle.load() for reading from files.
By the end of this lesson, students will understand how to serialize and deserialize Python objects using the pickle module. They will learn the concepts of pickling and unpickling, allowing them to save complex data structures to files and retrieve them later.
1. Introduction to pickling:
Pickling is the process of converting a Python object (such as a list, dictionary, or custom class instance) into a byte stream that can be saved to a file or transmitted over a network. This is useful for persisting Python objects across program executions or sending them between different systems.
2. The pickle module:
The pickle module in Python provides a simple way to serialize and deserialize Python objects. It includes methods like pickle.dump() for writing to files and pickle.load() for reading from files.
import pickle
3. Pickling Objects:
To pickle an object, you can use the pickle.dump() function to write the object to a binary file.
data = {"name": "Alice", "age": 30, "courses": ["Math", "Science"]} with open("data.pkl", "wb") as file: pickle.dump(data, file)
In this example, the dictionary data is serialized and saved to a file named data.pkl. The wb mode indicates that the file is opened in binary write mode.
4. Unpickling objects:
Unpickling is the reverse process, where a byte stream is converted back into a Python object using the pickle.load() function.
with open("data.pkl", "rb") as file: loaded_data = pickle.load(file) print(loaded_data) # Output: {'name': 'Alice', 'age': 30, 'courses': ['Math', 'Science']}
Here, the data stored in data.pkl is read and converted back to its original dictionary form.
5. Pickling custom objects:
You can also pickle custom class instances. This allows you to save the state of an object to a file.
class Student: def __init__(self, name, age): self.name = name self.age = age student = Student("Bob", 25) with open("student.pkl", "wb") as file: pickle.dump(student, file)
To unpickle the object:
with open("student.pkl", "rb") as file: loaded_student = pickle.load(file) print(loaded_student.name, loaded_student.age) # Output: Bob 25
6. Handling Exceptions:
When working with pickling, it is important to handle exceptions that may arise, such as when trying to unpickle an incompatible or corrupted file.
try: with open("corrupted.pkl", "rb") as file: data = pickle.load(file) except (EOFError, pickle.UnpicklingError) as e: print(f"Error unpickling file: {e}")
7. Practical use cases for pickling:
Pickling is useful in various scenarios, including:
- Saving machine learning models for later use.
- Storing user preferences or application settings.
- Transmitting complex data structures over networks.
8. Important considerations:
- Security: Be cautious when unpickling data from untrusted sources, as it may lead to arbitrary code execution.
- Compatibility: Pickled data may not be compatible across different Python versions. It is best to use the same version for pickling and unpickling.
9. Practical examples and exercises:
Exercise 1: Basic pickling
1. Create a list of your favorite books and pickle it to a file.
2. Unpickle the list and print its contents.
Exercise 2: Custom class pickling
1. Define a simple class (e.g., Car) with attributes such as make, model, and year.
2. Create an instance of the class, pickle it, and then unpickle it to display its attributes.
Exercise 3: Exception handling
1. Try to unpickle a non-existent file and handle the exception gracefully.
2. Print a user-friendly message if an error occurs.
Exercise 4: Pickling nested structures
1. Create a dictionary that contains lists and other dictionaries as values.
2. Pickle and unpickle this structure, printing the original and loaded data to verify correctness.
Exercise 5: Analyzing Pperformance
1. Compare the size of a pickled object with its original representation (e.g., JSON).
2. Discuss when to use pickling versus other serialization methods like JSON.
Conclusion
In this lesson, students learned about pickling and unpickling objects in Python using the pickle module. They explored how to serialize complex data structures and the considerations necessary when using pickling. Understanding these concepts is vital for effectively managing data persistence in Python applications.