pickle.dumps() vs pickle.loads()
pickle.dumps() and pickle.loads() are both Python Pickle Module APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. pickle.dumps(): Serializes a Python object to bytes (instead of file). Its syntax is pickle.dumps(obj, protocol=None, *, fix_imports=True), and it returns bytes pickle.loads(): Deserializes a Python object from bytes (instead of file). Its syntax is pickle.loads(data, *, fix_imports=True, encoding="ASCII", errors="strict"), and it returns deserialized object Example of pickle.dumps(): data = pickle.dumps({"key": [1,2,3]})
type(data) # bytes. Example of pickle.loads(): obj = pickle.loads(pickle.dumps([1,2,3]))
# obj = [1,2,3]. Read the full references for pickle.dumps() and pickle.loads() to see parameters, edge cases, and example code side by side. Other Pickle Module APIs worth comparing: pickle.dump(), pickle.load(), pickle.HIGHEST_PROTOCOL. Both belong to the Pickle Module category in the Python Reference documentation. Try both in the Python playground to see the behavioral difference live.