On this page

Serialization with Pickle

Enrico Hochkoeppler Profile

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:

Pickle is powerful, but it must be used carefully due to security considerations.


The pickle Module​

Pickle works with two core operations:

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:


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:


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:

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:

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:

For modern applications, HIGHEST_PROTOCOL is recommended.


Best Practices for Using Pickle​

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!

Buy Me a Coffee

Previous

Working with CSV

Next

Guided Exercise: Build a Persistent To‑Do Manager