Sub topic 11
Record What You Learned
This chapter assumes you can read basic Python, have used a chat assistant or terminal assistant, and can run small snippets locally. It also assumes you have already asked an assistant to explain some code and checked its answer. The last step is to keep what you learned, so you don't have to work it out again in three weeks.
Why record it
An explanation you only read is easy to forget. You will probably meet the same code again, and so will your teammates. A short written record saves the next reader from repeating the whole prompt, answer, and verification loop.
Record what you verified, not everything the assistant said. A chat transcript is long and mixes correct claims with wrong ones. A note should hold only the parts you checked.
Choose where the note goes
Pick the place based on who needs it and how long it should live.
| Where | Best for | Lifetime |
|---|---|---|
| Code comment | Why a line exists, a non-obvious behavior | Lives with the code |
| Docstring | What a function does, its inputs and outputs | Lives with the code, shows in help tools |
| Personal notes | Your own learning, links, open questions | Yours only |
| Project docs or README | Facts the whole team needs | Shared |
| Commit message or pull request | Why a change was made | Tied to a change |
A good rule is to put the note as close to the code as possible. A comment next to the decorator is more likely to be read, and more likely to be updated, than a page in a wiki.
What a good note contains
A useful note answers four things:
- What the code does, in one sentence.
- Why it is written that way.
- How you checked it, such as a command you ran or a documentation page.
- What you are still unsure about.
Item 3 matters most. A note that says "verified with cache_info()" tells the reader how much to trust it. Item 4 keeps you honest about gaps, such as behavior that depends on Python version.
Worked example
Earlier you asked an assistant about this function and then confirmed its claims by running it:
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
print(fib(30))
print(fib.cache_info())The run reports 31 misses and 28 hits. That matches the assistant's claim that each value of n from 0 to 30 is computed once and then reused. Now write that down as a docstring and comment in the code:
from functools import lru_cache
@lru_cache(maxsize=None) # unbounded cache: memory grows with each distinct n
def fib(n):
"""Return the nth Fibonacci number.
Results are cached by lru_cache, so each n is computed once.
Without the cache, the recursion repeats work and takes exponential time.
Checked with fib.cache_info(): fib(30) gives 31 misses and 28 hits.
Arguments must be hashable, because they are used as cache keys.
"""
return n if n < 2 else fib(n - 1) + fib(n - 2)
if __name__ == "__main__":
print(fib(30))
print(fib.cache_info())Each line of the note ties back to something you checked. The comment on maxsize=None records a trade-off. The docstring records the evidence, so a reviewer can rerun it.
You can also ask the assistant to draft the note. Give it your verified findings and ask it to format them, and tell it not to add new claims:
Turn these verified findings into a docstring for fib. Use only the facts below and do not add new claims.
- lru_cache with maxsize=None caches every result.
- fib(30) reports 31 misses and 28 hits in cache_info().
- Arguments must be hashable.Then read the draft. An assistant can slip in an unverified detail even when told not to.
Keep notes accurate
Notes go stale. A comment that no longer matches the code is worse than no comment, because readers trust it.
- Describe the behavior and the reason, not line-by-line mechanics that the code already shows.
- Mention versions when behavior depends on them.
- Update the note in the same change that edits the code.
- Mark unresolved items clearly, for example
TODO: confirm behavior on Python 3.8.
Common mistakes
- Saving the whole chat instead of a short summary.
- Recording claims you did not test.
- Putting notes somewhere far from the code, where nobody looks.
- Leaving out the open questions, which makes the note look more certain than it is.
- Including secrets or private data from your prompt in a comment or note.
Exercise
You asked an assistant to explain a function and tested one of its claims by running the code. It also made a second claim about behavior on older Python versions, which you could not check. What should your code comment say?
Show answer
Record the claim you tested and how you tested it. Mark the second claim as unverified, for example with a TODO such as "TODO: confirm behavior on older Python versions". Do not state it as fact.
Exercise
Name two reasons to put a note in a docstring or comment rather than only in your personal notes.
Show answer
It stays next to the code, so future readers see it where they need it. It can be updated in the same change as the code, which keeps it from going stale. It is also shared with teammates, while personal notes are not.