I've started to learn about the pickle module used for object serialization and deserialization.
I know that pickle.dump
is used to store the code as a stream of bytes (serialization), and pickle.load
is essentially the opposite, turning a stream of bytes back into a python object. (deserialization).
But what are pickle.dumps
and pickle.loads
, and what are the differences between them and pickle.dump
and pickle.load
? I've looked at the documentation, but I am having trouble differentiating between the two.
pickle.dumps(obj, protocol=None, *, fix_imports=True, buffer_callback=None) ; Return the pickled representation of the object obj as a bytes object, instead of writing it to a file.
pickle.loads(data, /, *, fix_imports=True, encoding="ASCII", errors="strict", buffers=None) Return the reconstituted object hierarchy of the pickled representation data of an object. data must be a bytes-like object.
The difference between dump
and dumps
is that dump
writes the pickled object to an open file, and dumps
returns the pickled object as bytes
. The file must be opened for writing in binary mode. The pickled version of the object is exactly the same with both dump
and dumps
.
So, if you did the following for object obj
:
with open("pickle1", "wb") as f:
pickle.dump(obj, f)
with open("pickle2", "wb") as f:
f.write(pickle.dumps(obj))
you'd end up with two files with exactly the same contents.
The same applies to loading - load
"unpickles" from an open (readable) file object, and loads
uses a bytes
object.