Python-native caching
Caching computed Python values between process runs.
Python · Module
pickle serializes Python object hierarchies into binary streams and reconstructs them later. It is Python-specific and must only be used with trusted data because unpickling can execute code.
The module converts Python object hierarchies to byte streams and reconstructs them later. Use dumps and loads for byte values, or Pickler and Unpickler when file-oriented control is useful; the format is Python-specific and loading can execute code, so only load trusted data.
pickle.dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None)
pickle.dumps(obj, protocol=None, *, fix_imports=True, buffer_callback=None)
pickle.load(file, *, fix_imports=True, encoding='ASCII', errors='strict', buffers=None)
pickle.loads(data, /, *, fix_imports=True, encoding='ASCII', errors='strict', buffers=None)| Name | Type | Description |
|---|---|---|
obj | object | Object to serialize. |
file | file-like object | Binary file object receiving the pickle. |
data | bytes-like object | Pickle byte stream to deserialize. |
dumps returns a pickled representation as bytes; loads returns the reconstituted object hierarchy; dump and load operate on file objects.
import pickle
from pathlib import Path
record = {"name": "Ada", "roles": ["engineer"]}
path = Path("record.pickle")
with path.open("wb") as file:
pickle.dump(record, file)
with path.open("rb") as file:
restored = pickle.load(file)
assert restored == record
path.unlink()Writes a picklable dictionary to a binary file and restores it with the matching load operation.
Caching computed Python values between process runs.
Saving application state when both writer and reader are Python programs.
Transmitting complex Python object graphs through a trusted internal channel.
Use pickle.dumps(obj) to serialize an object hierarchy to bytes and pickle.loads(data) to reconstruct it. For file-based workflows, pickle.dump(obj, file) and pickle.load(file) provide the corresponding operations.
Follow-up: Why should pickle not be used for untrusted input? Loading a pickle can execute arbitrary code, so the input must be authenticated or otherwise trusted.
Pickle supports multiple binary protocols, and the default protocol is designed for compatibility while newer protocols can provide newer capabilities. The protocol choice affects which Python versions can read the resulting stream.
Follow-up: How can you inspect a pickle without loading it? Use pickletools to disassemble and analyze the pickle stream rather than reconstructing its objects.
Pickle can serialize many built-in types, including numbers, strings, bytes, bytearrays, tuples, lists, sets, and dictionaries containing picklable objects. It can also preserve shared references within an object hierarchy.
Follow-up: What commonly prevents an object from being pickled? Objects such as open file handles, sockets, and active execution state generally need to be excluded or represented through custom reduction logic.
pickletoolsPicklerUnpicklerPickleBuffercopyreg