Alternative Data Formats: JSON and CSV
Persistence does not mean that the original in-memory variable remains alive. It means that enough information has been stored in a file for the object to be reconstructed later.
When Memory Is Not Enough
A Python object can exist in memory while a program is running, but that existence is temporary. When the program ends, the object is no longer available in memory. Data persistence solves this problem by storing a Python object in a file so it can survive program termination. Pickle is the mechanism described in this lesson for moving an object from temporary memory into persistent file storage and later bringing it back.
The Persistence Boundary
RAM and file storage play different roles. An object in RAM is fast to access and modify, but it is volatile: it disappears when the program ends or the computer loses power. A file on disk is persistent: it survives program termination and power loss. Pickle bridges these two forms of storage. It converts the object into data that can be written to a file, and the file can later be used to reconstruct the object in memory.
| Location | State of the data | What happens after program termination |
|---|---|---|
| RAM | Volatile | The object disappears from memory |
| File on disk | Persistent | The stored data remains available |
The source distinguishes temporary objects in RAM from persistent file storage.
Persistence does not mean that the original in-memory variable remains alive. It means that enough information has been stored in a file for the object to be reconstructed later.
Saving an Object with Pickle
Pickling is the saving stage. The pickle module analyzes the object's type, structure, and nested values, then serializes that information into a binary format. The resulting bytes are written to a file with pickle.dump(). The file must be opened in write binary mode for this operation.
- 1Import the pickle module.
- 2Create or identify the Python object to save.
- 3Open a file in write binary mode.
- 4Pass the object and the file to
pickle.dump(). - 5Close the file after the object has been stored.
Restoring the Original Object
Unpickling is the retrieval stage. pickle.load() reads the bytes from a pickle file, decodes the binary data, and reconstructs the object in memory. The file must be opened in read binary mode. The restored object is identical to the original in type, structure, and values.
- 1Open the stored file in read binary mode.
- 2Pass the file to
pickle.load(). - 3Allow pickle to read and decode the bytes.
- 4Receive the reconstructed Python object in memory.
The Shopping List Cycle
Saving, deleting, and restoring a shopping list
Follow what happens when a shopping list is stored in a file, removed from memory, and then retrieved.
- 1Create
A shopping list is created as a Python list in memory.
- 2Pickle
The file
shoplist.datais opened in write binary mode, andpickle.dump()stores the list in it. - 3Remove
The shoplist variable is deleted, so the original variable no longer exists in memory.
- 4Unpickle
The same file is opened in read binary mode, and
pickle.load()retrieves the stored list. - 5Restore
The retrieved object is a list with the same type, structure, and values as the original.
The shopping list is recovered from the file even though the original shoplist variable was deleted.
What do you think happens?
After the shoplist variable is deleted, can the shopping list still be recovered?
Reveal answer
Yes, because the stored file contains the serialized object.
pickle.dump() stored the list in shoplist.data before the variable was deleted. pickle.load() later reads that file and reconstructs the list in memory.
Common Persistence Mistakes
Common mistake Treating pickle.dump() as if it only writes a text version of the object
What it looks likeAssuming the file simply contains the ordinary printed representation of a list
Why it failsPickling performs serialization. It analyzes the object's type, structure, and nested values and encodes them into a binary format.
Do this insteadThink of pickle.dump() as converting the complete object into bytes for storage.
Common mistake Using the wrong file mode for saving
What it looks likeOpening the destination file without write binary mode before calling pickle.dump()
Why it failsThe source specifies write binary mode for pickling.
Do this insteadOpen the file in write binary mode before storing the object.
Common mistake Using the wrong file mode for loading
What it looks likeOpening the stored file without read binary mode before calling pickle.load()
Why it failsThe source specifies read binary mode for unpickling.
Do this insteadOpen the file in read binary mode before retrieving the object.
Common mistake Believing that deleting the variable also deletes the saved data
What it looks likeAssuming that removing shoplist from memory removes the contents of shoplist.data
Why it failsThe source example deletes the variable after saving and still restores the list from the file.
Do this insteadSeparate the in-memory variable from the persistent file that contains its serialized data.
A Reliable Save-and-Restore Routine
Keep the two halves of the process distinct. During saving, start with the object in memory, open a file in write binary mode, and use pickle.dump(). During restoration, start with the stored file, open it in read binary mode, and use pickle.load(). Closing the file after saving is part of the demonstrated cycle. This separation makes it easier to identify whether a problem occurred while writing the persistent representation or while reconstructing the object.
| Stage | Starting point | Operation | Result | File mode |
|---|---|---|---|---|
| Pickling | Python object in memory | pickle.dump() | Bytes stored in a file | Write binary |
| Unpickling | Bytes in a file | pickle.load() | Python object in memory | Read binary |
Pickling and unpickling reverse the direction of the data movement.
Check Your Understanding
A program creates a Python object, saves it to a file with pickle.dump(), ends, and starts again later. Describe the file mode, function, and data movement required in each stage.
Hints
- Saving and retrieving use different binary file modes.
pickle.dump()moves an object toward a file.pickle.load()moves stored data back into memory.
Explain why deleting a variable does not prevent recovery when the object was already pickled to a file.
Hints
- Compare the variable in RAM with the serialized data on disk.
- Consider what
pickle.load()reads.
Persistence in One View
- 1Data persistence stores Python objects in files so they survive program termination.
- 2RAM is volatile, while a file on disk is persistent.
- 3
pickle.dump()serializes an object into bytes and writes those bytes in write binary mode. - 4
pickle.load()reads the bytes in read binary mode and reconstructs the original object. - 5The reconstructed object has the same type, structure, and values as the original.
Key Takeaways
- Persistence allows data to survive after a program terminates.
- Pickling moves a Python object from volatile memory into a persistent binary file.
- Unpickling reads that file and reconstructs the object in memory.
- Saving uses
pickle.dump()with write binary mode; retrieval usespickle.load()with read binary mode. - Deleting the original variable does not remove the serialized data already stored in the file.