...

/

Pickling Without a File

Pickling Without a File

We'll cover the following...

The examples in the previous section showed how to serialize a Python object directly to a file on disk. But what if you don’t want or need a file? You can also serialize to a bytes object in memory.

Press + to interact
import pickle
shell = 1
print (shell)
#1
with open('entry.pickle', 'rb') as f:
entry = pickle.load(f)
b = pickle.dumps(entry) #①
print (type(b)) #②
#<class 'bytes'>
entry3 = pickle.loads(b) #③
print (entry3 == entry) #④
#True

① The ...