- Courses
- 💻Programming Languages
- 🐍Python
- File Handling & Persistence
- Serialization with Pickle
On this page
Serialization with Pickle
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
What Serialization Means
Serialization is the process of converting a Python object into a format that can be stored or transmitted, then reconstructed later.
Pickle provides Python’s built‑in mechanism for serializing almost any Python object, including:
- Lists, dicts, sets
- Custom class instances
- Functions (with limitations)
- Complex nested structures
Pickle is powerful, but it must be used carefully due to security considerations.
The pickle Module
Pickle works with two core operations:
- pickling — converting an object into bytes
- unpickling — reconstructing the object from bytes
Pickle stores data in binary format, so files must be opened in binary mode.
Writing (Pickling) Python Objects
Use pickle.dump() to serialize an object to a file.
import pickle
data = {
"name": "Enrico",
"languages": ["Python", "TypeScript"],
"active": True
}
with open("data.pkl", "wb") as f:
pickle.dump(data, f)
Key points:
"wb"= write binarypickle.dump(obj, file)writes the serialized bytes
Reading (Unpickling) Python Objects
Use pickle.load() to reconstruct the object.
import pickle
with open("data.pkl", "rb") as f:
data = pickle.load(f)
Now data is a fully restored Python object with the same structure and values.
Serializing to and from Bytes (In‑Memory)
Pickle can also work without files.
Convert object → bytes
raw = pickle.dumps(data)
Convert bytes → object
restored = pickle.loads(raw)
This is useful for:
- Network transmission
- Caching
- Storing objects in memory‑based systems
Pickling Custom Objects
Pickle can serialize instances of user‑defined classes automatically.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p = Person("Alice", 30)
with open("person.pkl", "wb") as f:
pickle.dump(p, f)
Unpickling restores the instance:
with open("person.pkl", "rb") as f:
p2 = pickle.load(f)
p2 is a fully reconstructed Person object.
Versioning and Compatibility
Pickle stores references to class names and module paths.
This means:
- If you rename a class → old pickles break
- If you move a class to another module → old pickles break
- If you change attribute names → old pickles may break
Pickle is best used for short‑term persistence, not long‑term archival.
Security Warning
Pickle is not safe for untrusted data.
Unpickling arbitrary bytes can execute arbitrary code.
Never unpickle data from unknown or untrusted sources.
For secure, interoperable formats, prefer:
- JSON
- CSV
- Protocol Buffers
- MessagePack
Pickle is ideal only when you control both serialization and deserialization.
Choosing a Pickle Protocol
Pickle supports multiple binary formats called protocols.
pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
Higher protocols:
- Produce smaller files
- Are faster
- May not be compatible with older Python versions
For modern applications, HIGHEST_PROTOCOL is recommended.
Best Practices for Using Pickle
- Use pickle only with trusted data sources
- Use binary modes
"wb"and"rb" - Prefer
HIGHEST_PROTOCOLfor performance - Avoid pickling objects that depend on external state
- Avoid pickling for long‑term storage
- Document class changes if pickled data must persist
These practices ensure safe, predictable serialization.
Summary
In this lesson, you learned how to serialize and deserialize Python objects using the pickle module. You explored writing and reading pickled files, working with bytes, pickling custom objects, protocol versions, and critical security considerations.
Pickle is a powerful tool for short‑term persistence and fast object storage—when used responsibly.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Working with CSV
Next
Guided Exercise: Build a Persistent To‑Do Manager
-
What Serialization Means
-
The
pickleModule -
Writing (Pickling) Python Objects
-
Reading (Unpickling) Python Objects
-
Serializing to and from Bytes (In‑Memory)
- Convert object → bytes
- Convert bytes → object
-
Pickling Custom Objects
-
Versioning and Compatibility
-
Security Warning
-
Choosing a Pickle Protocol
-
Best Practices for Using Pickle
-
Summary
