Sub topic 11
Record what you learned
You asked a specific question, gave the assistant context, checked its answer, and ran the code. Now you understand something that you didn't before. That understanding fades quickly unless you write it down. This chapter covers how to turn an explanation session into notes, code comments, and tests that you or a teammate can use later.
This chapter assumes you have read the earlier chapters on prompting and verification. You should be able to run small Python snippets locally. The examples use the lru_cache function from earlier in the tutorial.
Why capture understanding
An explanation session produces a lot of text. Most of it is scaffolding: your questions, the assistant's restatements, and wrong turns. What you need to keep is small:
- What the code does, in your own words.
- Why it is written that way.
- What you verified and how.
- What is still uncertain.
If you skip this step, you pay for the same understanding again. In three weeks you will meet the same decorator, search your memory, and start a new chat. A teammate who reads the same file will start from zero.
Writing also tests your understanding. If you cannot restate the explanation without copying the assistant's wording, you probably don't understand it yet. Go back to the follow-up questions before you record anything.
Choose where the knowledge belongs
Different kinds of knowledge belong in different places. Choose by asking who needs it and when.
| What you learned | Where to put it | Who benefits |
|---|---|---|
| What a specific function does and why | A comment or docstring next to the code | Anyone reading that code |
| A behavior you checked by running code | A test | Anyone changing the code, including the future you |
| A concept you will meet in many files | A personal or team notes page | You and your team |
| Why a decision was made | A commit message or design note | Reviewers and maintainers |
| Open questions you could not resolve | A TODO or an issue | Whoever picks it up |
A comment is the right place for knowledge tied to one line of code. A test is better when the knowledge is a claim that could become false. A note is better for general concepts, such as "what memoization is", that do not belong in any one file.
Write notes in your own words
Do not paste the assistant's whole answer into your notes. Long pasted answers are hard to scan, and they may contain claims you never checked. Instead, write a short entry that you could read in thirty seconds.
A useful entry has a fixed shape. This one works for most code you explain:
Topic: functools.lru_cache on fib(n)
Date: 2026-11-27
Source file: utils/math_helpers.py
What it does:
Stores the result of each call keyed by the argument. A repeated call
with the same n returns the stored result instead of recomputing.
Why it is here:
fib calls itself twice. Without the cache the number of calls grows
exponentially with n. With it, each n is computed once.
Checked by:
Ran fib(30) and printed fib.cache_info(): 31 misses, 28 hits.
Caveats:
- Arguments must be hashable.
- maxsize=None means the cache never evicts, so memory grows.
- Deep recursion can still hit Python's recursion limit.
Still unsure:
- Behavior with threads (not tested).Notice three things about this entry. The "Checked by" line says how you know, not just what you know. The "Caveats" section holds the edge cases that surprised you. The "Still unsure" section is explicit, so a reader will not mistake an untested claim for a verified one.
Separate verified claims from assistant claims
When you copy something from an assistant, mark whether you confirmed it. A simple convention is enough:
- Write "Checked" with the method for anything you ran or read in official documentation.
- Write "Unverified" for anything you took from the assistant on trust.
This matters because notes age. A note that says "lrucache is thread-safe" with no source will be repeated as fact. A note that says "Unverified: assistant said lrucache is thread-safe" tells the next reader to check first.
Turn the explanation into a code comment
A good comment records the reason, not the mechanics. The code already shows that fib calls itself. A comment should say why the decorator is there and what limits it has.
Compare two comments for the same function.
from functools import lru_cache
# Weak: repeats what the code says.
# This function uses lru_cache and computes fibonacci numbers.
@lru_cache(maxsize=None)
def fib_weak(n):
return n if n < 2 else fib_weak(n - 1) + fib_weak(n - 2)
# Better: records the reason, the verified evidence, and the limits.
# Memoized so each n is computed once. Without the cache the recursion
# makes an exponential number of calls.
# Checked: fib(30) gives 31 misses and 28 hits in fib.cache_info().
# Limits: n must be hashable, maxsize=None never evicts entries, and
# very large n can still raise RecursionError.
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
if __name__ == "__main__":
print(fib(30))
print(fib.cache_info())The second comment is longer, but each line answers a question a future reader would otherwise have to ask. Keep it that way. If a comment grows past a short paragraph, move the detail into a docstring or a notes page and leave a pointer.
Avoid comments that will rot
Comments go stale when the code changes and nobody updates them. Reduce the risk:
- Do not write "the assistant said" in a code comment. Record the fact, not its source.
- Do not copy version-specific claims without naming the version. "Works on Python 3.12" can be checked. "Works in modern Python" cannot.
- If a claim is checkable, prefer a test over a comment. A failing test is noticed. A wrong comment is not.
Turn verified behavior into a test
In the verification chapter you ran fib.cache_info() by hand. You can keep that check by turning it into a test. Now the claim "results are cached" is something the project enforces, not something you remember.
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
def test_fib_computes_each_value_once():
fib.cache_clear()
assert fib(30) == 832040
info = fib.cache_info()
assert info.misses == 31
assert info.hits == 28
def test_repeat_call_is_a_cache_hit():
fib.cache_clear()
fib(10)
before = fib.cache_info().hits
fib(10)
assert fib.cache_info().hits == before + 1
if __name__ == "__main__":
test_fib_computes_each_value_once()
test_repeat_call_is_a_cache_hit()
print("all checks passed")Two details are worth noting. Each test calls cache_clear() first, because the cache lives on the function and persists between tests. Without that reset, a test that passes alone can fail when run after another one. The second test checks the behavior directly: calling fib(10) again adds exactly one hit and does no new computation.
You can run this file with python as shown, or with pytest if your project uses it. Both work because the test functions are plain functions with assert statements.
What makes a test worth keeping
Not every thing you learn needs a test. Keep a test when the claim:
- Could become false if someone changes the code.
- Is cheap to check, without network, files, or long runtimes.
- Surprised you, since surprises are where misunderstandings hide.
Ask the assistant to help you capture, then edit
An assistant is good at drafting notes from a session you just finished. It is poor at deciding what you verified, because it cannot see what you ran. Use it for the draft and do the judgment yourself.
A prompt that works:
Based on our conversation about the lru_cache decorator on fib, draft a
short note with these sections: What it does, Why it is here, Caveats.
Use at most 120 words. Do not add any claim we did not discuss. Mark
every claim as either "I ran this" or "from the assistant" based on what
I told you I ran. I told you I ran fib(30) and read cache_info().Then review the draft:
- Delete any sentence you cannot explain.
- Check that every "I ran this" claim matches what you actually ran.
- Check numbers and function names against the code.
- Rewrite awkward phrases in your own words.
Assistants often add plausible-sounding detail while summarizing, such as a performance figure you never measured. Ask for "no claims we did not discuss", and still check the result.
Keep records for multi-file explanations
For code that spans many files, a single note per function is not enough. Record the path through the code instead. A short entry that lists entry point, key files, and the order of calls is often more useful than a detailed summary of each file:
Request flow for the export job
1. cli/export.py: main() parses arguments, calls run_export()
2. jobs/export_job.py: run_export() loads config, builds a Reader
3. io/reader.py: Reader.stream() yields rows in batches
4. jobs/export_job.py: writes each batch with the Writer
Checked: followed with a debugger, set breakpoints at steps 1 to 3.
Not checked: error handling when the Writer fails.Record the date and the commit or version you read. A flow note is only true for the code as it was. A commit hash lets a later reader tell whether the note might be out of date.
Common errors
- Copying the whole chat. Long transcripts are never reread. Write a short entry instead.
- Recording unverified claims as facts. Mark them as unverified, or check them first.
- Writing comments that restate the code. Record why, evidence, and limits.
- Leaving out the date or version. Behavior can differ across Python versions and library releases, and a note without a version cannot be checked.
- Pasting sensitive details into notes. Notes get shared. Apply the same rules about secrets and private code that you used in prompts.
- Skipping the "still unsure" list. A visible gap invites a fix. A hidden gap becomes a surprise later.
A short checklist
Before you close the session, check each item:
- I can state what the code does and why, without looking at the assistant's answer.
- Every claim I recorded is marked as checked or unverified.
- Behaviors that could break are covered by a test.
- Comments explain why and list limits, not mechanics.
- Open questions are written down somewhere a person will see them.
- Nothing in the notes contains secrets or private data.
Exercise
You asked an assistant about a retry decorator in your project. It told you the decorator waits twice as long after each failure, and you confirmed this by running the code with a fake function that always fails and printing the delays. It also said the decorator is "safe to use with async functions", which you did not test. Where should you record each of these two claims, and how should you word them?
Show answer
Record the backoff behavior as a test that calls a failing function, captures the delays (for example by patching the sleep function), and asserts that each delay is double the previous one. Add a short comment that says why the backoff exists. Record the async claim as unverified: write "Unverified: assistant said this works with async functions" in your notes, or open a TODO or issue to check it. Do not put it in a code comment as a fact, because you have no evidence for it.
Exercise
Why does the test in this chapter call fib.cache_clear() at the start of each test function?
Show answer
The cache is stored on the decorated function, so it persists between calls and between tests. If an earlier test already computed fib(30), a later test that expects 31 misses would see zero misses and fail. Clearing the cache gives each test a clean starting state, so the result does not depend on test order.