liveBook · Manning

Byte stream

As explained in The Quick Python Book, Fourth Edition

A byte stream is an ordered sequence of bytes representing serialized data or other binary content.

A byte stream is an ordered sequence of bytes used to represent serialized data or other binary content. In Python, pickling converts an object hierarchy into a byte stream that can be stored in a binary file or moved elsewhere; unpickling reverses the process. section 13.8

When used for serialized Python data, a byte stream supports these operations and limits:

  • Store numbers, strings, collections, class instances, shared objects, and cyclic references. section 13.8
  • Write and read multiple serialized values in sequence with pickle.dump and pickle.load. section 13.8
  • Exclude code objects and system resources such as files or sockets from pickling. section 13.8

Persisting program state

Open a file in binary-write mode, dump values into the resulting byte stream, then reopen it in binary-read mode and load the values in the same order. The restored values are a=42, b=3.14, and c='test'.

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines serialization and deserialization, demonstrates sequential file operations, and notes unsupported code objects and system resources.
Examples
Uses scalar variables, dictionaries, saved program state, and a disk-backed calculation cache.
What it assumes
Assumes familiarity with Python variables, objects, files, and binary file modes while defining serialization terminology.
What you can do after
Apply it via Python's pickle module to save and restore collections or selected program state.
How it connects
Connects byte streams with pickling, unpickling, object hierarchies, binary files, and bytes-like objects.

Also explained in

Compare how 3 books explain byte stream →

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.