Home > Blockchain >  pickle.dump/load vs pickle.dumps/loads
pickle.dump/load vs pickle.dumps/loads

Time:09-21

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 bits (serialization), and pickle.loads is making a stream of bits back into the python object. (deserialization). But what are dumps and loads, and what are the differences between them and dump and 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.

Appreciate any help, Thank you

CodePudding user response:

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.

  • Related