What Python module implements binary protocols for serializing and deserializing Python objects?

The story behind the answer

The Python module that implements binary protocols for serializing and deserializing Python objects is pickle.

Pickling converts a Python object hierarchy into a byte stream; unpickling reconstructs the objects from that stream. Unlike JSON, pickle can preserve many Python-specific structures, including object graphs and certain custom class instances, making it useful for Python-to-Python persistence and data transfer.

The module supports multiple protocol versions. Newer protocols generally provide more efficient representations, while older protocols can help with compatibility between Python releases. The format is Python-specific, so programs written in other languages usually cannot directly reconstruct arbitrary pickled objects.

A frequent confusion is marshal, which also writes binary data but primarily supports Python’s internal storage needs, including compiled code. YAML and JSON are text-oriented formats with broader interoperability. Most importantly, pickle is not a secure format: never unpickle data from an untrusted or tampered source, because loading it can execute harmful code.

Source: Wikipedia · fact-checked Aug. 2026

Add question to a list

Choose a list to keep this question in: