On this page
  1. marshal
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Why marshal Exists Alongside pickle
  7. Gotchas & Boundaries
  8. Common Functions
  9. Constants & Attributes

marshal

Prev: mailbox | Up: stdlib_module | Next: math
Rel: pickle, copyreg, shelve, dbm, sqlite3
Ref: docs.python.org — marshal
Tags: #public #python3 #stdlib


Import

import marshal

Overview

CPython's internal binary serializer (dumps(), loads(), dump(), load()) used exclusively for reading and writing .pyc bytecode files (code objects and basic built-in primitives). Never use marshal for general application persistence or data exchange—use json, sqlite3, or pickle instead.

Quick Start

import marshal

data = {"version": 1, "flags": (True, False), "items": [10, 20, 30]}
blob = marshal.dumps(data)
assert marshal.loads(blob) == data

Implementation Walkthrough

1. Why marshal Exists Alongside pickle

Unlike pickle, marshal can serialize Python code objects (types.CodeType, produced by compile()) directly, which is how .pyc bytecode caches work, and it runs faster for simple literals—at the cost of zero cross-version format stability.

Gotchas & Boundaries

Common Functions

Constants & Attributes

Linked from

Related notes