Avoid eager fallback work in dict.setdefault
A dictionary cache can return an existing value and still run an unnecessary loader. In cache.setdefault(key, load_label(key)), Python calls the loader before the dictionary method sees the arguments. Use an explicit missing-key branch when the fallback should run only on a miss.
Set up
Tested on Linux with CPython 3.12.14. Use Bash, Python 3.12 or newer, a text editor, and the mktemp, rm and rmdir utilities. No packages or network access are needed. Keep the same terminal open and stop if setup fails. Run without -O so the assertions remain enabled.
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1Run the example
Save as lazy_dictionary_default.py:
calls = []
def load_label(key):
calls.append(key)
return f"loaded:{key}"
cache = {"report": "cached:report", "empty": None}
value = cache.setdefault("report", load_label("report"))
assert value == "cached:report"
assert calls == ["report"]
print("Existing value:", value)
print("Fallback calls:", calls)
calls.clear()
def get_or_load(key):
if key not in cache:
cache[key] = load_label(key)
return cache[key]
assert get_or_load("report") == "cached:report"
assert get_or_load("empty") is None
assert calls == []
assert get_or_load("invoice") == "loaded:invoice"
assert get_or_load("invoice") == "loaded:invoice"
assert calls == ["invoice"]
print("Lazy calls:", calls)
print("Cached None:", get_or_load("empty") is None)Run:
python3 -B lazy_dictionary_default.pyObserved output:
Existing value: cached:report
Fallback calls: ['report']
Lazy calls: ['invoice']
Cached None: TrueSeparate argument evaluation from insertion
The first lookup returns the cached label, but the recorded calls show that the loader still ran. Its return value was not inserted because the key already existed. In a real loader, a discarded result can still mean an unnecessary query or an unwanted side effect.
The helper checks membership before calling the loader. Repeated invoice lookups load once, while the existing null value remains a cache hit. A truthiness test would treat some legitimate cached values as missing.
The dictionary reference describes insertion for an absent key. Python’s call-expression rules explain why the default expression has already run by then. This evaluation rule also applies to a function call supplied as the default argument to a normal dictionary lookup.
Define the cache’s concurrency contract
This helper uses an ordinary dictionary in one thread. The membership check, load and assignment form several operations. Another caller could observe the same miss and also load it. Shared caches need coordination appropriate to their threads or asynchronous tasks if duplicate work must be prevented.
If the loader raises before returning, this helper does not assign a new value. That does not undo external effects the loader already performed. The demonstration loader only records calls and returns a string; it performs no I/O.
Clean up
Remove the saved file and empty scratch directory. If the directory is not empty, inspect its contents before removing anything else.
rm -- lazy_dictionary_default.py
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirAcknowledgements
This post was created with assistance from AI.