Skip to main content

pickle

Binary serialisation of arbitrary Python object graphs. Five protocol versions; protocol 5 (PEP 574) adds out-of-band buffers.

Source-of-record: Lib/pickle.py, Modules/_pickle.c, pickle docs.

Functions

FunctionEffect
dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None)Write.
dumps(obj, protocol=None, *, fix_imports=True, buffer_callback=None)Bytes.
load(file, *, fix_imports=True, encoding='ASCII', errors='strict', buffers=None)Read.
loads(data, /, *, fix_imports=True, encoding='ASCII', errors='strict', buffers=None)Bytes in.

Classes

ClassPurpose
Pickler(file, protocol=None, *, fix_imports=True, buffer_callback=None)Stream pickler.
Unpickler(file, *, fix_imports=True, encoding='ASCII', errors='strict', buffers=None)Stream unpickler.
PickleBuffer(buffer)Out-of-band buffer holder.

Pickler.persistent_id(obj) and Unpickler.persistent_load(pid) implement object reference sharing across pickles. dispatch_table and reducer_override(obj) customise reduction.

Protocols

VersionNotes
0ASCII; printable.
1Old binary.
2New-style classes (3.0+).
3Bytes support (3.0+).
4Large objects, qualname (3.4+).
5Out-of-band buffers (3.8+).

HIGHEST_PROTOCOL = 5. DEFAULT_PROTOCOL = 5 since 3.8.

What can be pickled

None, True, False; numbers; str, bytes, bytearray; tuples, lists, sets, dicts of picklable values; functions and classes defined at module top level; objects whose __reduce__ / __reduce_ex__ cooperates.

Exceptions

PickleError (base), PicklingError, UnpicklingError.

Gopy status

AreaState
Protocols 0-5Complete.
PickleBuffer / out-of-bandComplete.
Pickler.dispatch_table, reducer_overrideComplete.

Reference

  • CPython 3.14: pickle.
  • Lib/pickle.py, Modules/_pickle.c.
  • module/_pickle/. gopy port.
  • PEP 574.