This Python tutorial explains shelve Module: Simple Persistent Dictionaries. Follow the example, inspect the output, and practise an input change so you understand the behavior rather than simply copying the syntax.
What This Feature Does
Persist a small Python mapping using the shelve module.
shelve stores pickled values in a DBM-backed dictionary-like database. Keys are strings and values are serialized with pickle; storage format and concurrent-write behavior depend on the backend. Only open trusted shelf files.
When to Use It
This technique belongs to persistent data and serialization. It is useful when the input, operation and expected outcome are known. Start with a small controlled example before integrating it into a larger workflow.
Step-by-Step Python Example
Read the code once before running it. Notice which values are inputs, which function or method performs the work, and whether the result is returned or stored by changing an existing object.
import shelve
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as directory_name:
database_path = str(Path(directory_name) / 'settings')
with shelve.open(database_path) as shelf:
shelf['theme'] = {'name': 'dark'}
print(shelf['theme']['name'])
Understanding the Code
- First, the imports make shelve, pathlib, tempfile available. They provide the filesystem, data or standard-library functions used later.
- Find the call TemporaryDirectory(). Its parameters determine the specific operation demonstrated in the title.
- Its purpose is to persist a small python mapping using the shelve module. Compare this description against the result printed by the program.
- The sample uses TemporaryDirectory() so any practice files are removed after the context ends; it does not work on your real documents.
- The print() expressions display a checkable outcome. Print values and exception names are not substitutes for the program result itself.
Expected Output
dark
The output is the observable result of the sample operation. Compare its value and type with the explanation, not just whether the program runs.
Important Details and Edge Cases
Save and reopen a tiny local settings shelf.
The shelve module uses pickle internally, so untrusted shelf files are unsafe to open.
Practice: Try It Yourself
For multi-user or relational applications, prefer a transactional database.
Begin by copying the example into a separate practice file. Change one input at a time, run it again, and describe why the result changed. If the exercise writes or deletes files, keep it inside a temporary test folder.
Official Python Documentation
For the complete reference, see the Python standard library documentation.
Common Beginner Mistakes
- Modifying a nested mutable object without reassigning it while writeback=False, or opening an untrusted shelf.
- Save and reopen a tiny local settings shelf.
- The shelve module uses pickle internally, so untrusted shelf files are unsafe to open.
Key Takeaways
- Persist a small Python mapping using the shelve module.
- shelve stores pickled values in a DBM-backed dictionary-like database. Keys are strings and values are serialized with pickle; storage format and concurrent-write behavior depend on the backend. Only open trusted shelf files.
- Avoid this common pitfall: Modifying a nested mutable object without reassigning it while writeback=False, or opening an untrusted shelf.