Python pickle Module
Example
Serialize a Python object to bytes and back:
import pickle
data = {"name": "Emil", "age": 30}
b = pickle.dumps(data, protocol=pickle.HIGHEST_PROTOCOL)
print(pickle.loads(b)["name"])
Try it Yourself »
Definition and Usage
The pickle module converts Python objects to a byte stream and restores them later.
Use it to save in-memory data for later use or transfer, but never unpickle data you don’t trust.
Members
Member | Description |
---|---|
DEFAULT_PROTOCOL | The default pickle protocol used when none is specified. |
dump() | Write a pickled representation of obj to a file-like object. |
dumps() | Return a pickled representation of obj as bytes. |
HIGHEST_PROTOCOL | The most recent pickle protocol supported by this Python. |
load() | Read a pickled object from a file-like object. |
loads() | Read a pickled object from a bytes-like object. |
PickleError | Base class for pickle-related errors. |
Pickler | Class that serializes objects to a file-like object. |
UnpickleableError | Error raised when an object cannot be pickled. |
Unpickler | Class that deserializes objects from a file-like object. |