liveBook · Manning

Unpickling

As explained in The Quick Python Book, Fourth Edition

Unpickling is the deserialization of a byte stream into a Python object hierarchy.

Unpickling converts a byte stream from a binary file or bytes-like object back into a Python object hierarchy. It restores saved program data with a small amount of code. section 13.8

Core rules:

  • Open the saved file in binary read mode and call pickle.load once for each object written. section 13.8
  • Restore lists, tuples, numbers, strings, dictionaries, class instances, shared objects, and cyclic references. section 13.8
  • Do not expect code objects or system resources such as files and sockets to be pickled. section 13.8

Restoring three saved values

with open("state", "rb") as file: a = pickle.load(file) b = pickle.load(file) c = pickle.load(file) This restores the values as a=42, b=3.14, and c='test'.

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines the inverse of pickling, demonstrates repeated loads, and notes supported and unsupported structures.
Examples
Uses sequentially restored scalar values, dictionary persistence, and a disk-backed calculation cache.
What it assumes
Assumes Python file handling and saved pickle data, while introducing object-hierarchy serialization and restoration.
What you can do after
Apply it via pickle.load to restore previously serialized values and reusable application data.
How it connects
Connects unpickling with pickling, serialization, binary files, Python objects, and caching.

Also explained in

Compare how 2 books explain unpickling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.