Sub topic 6
Read the answer critically
An assistant's explanation of unfamiliar code reads fluently whether it is right or wrong. This chapter covers how to read an answer as a set of separate claims, sort them by how much support each has, and pick the ones to check before you rely on them.
This chapter assumes you can read basic Python, have used a chat or terminal assistant, and can run small snippets locally. Later chapters cover follow-up questions, failure modes in more detail, and verification methods. Here the goal is to triage: read the answer, split it into claims, and mark the ones that need checking.
Why fluent is not the same as correct
A language model produces text that sounds plausible given your prompt and the code you pasted. Plausible and true overlap often, but not always. The prose of a wrong explanation is usually as confident as the prose of a right one, so tone tells you little about accuracy.
Reading critically does not mean distrusting everything. Most explanations of short, well-known code are mostly right. The skill is finding the few sentences where an error would cost you, and checking those.
Break the answer into claims
Take an explanation of this function, the running example in this tutorial:
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)Suppose you asked for an explanation and got this back:
This function computes the nth Fibonacci number recursively. The @lru_cache
decorator memoizes results, so each value of n is computed only once. Without
the cache, the function would take exponential time. With it, the runtime is
linear in n. Setting maxsize=None means the cache is unbounded, so the least
recently used eviction never happens. For fib(5), the calls reach fib(1) and
fib(0) first, and later calls for the same arguments return stored values.
The cache is shared across threads and processes, so parallel workers will
reuse each other's results.Each sentence makes a separate claim. List them one per line:
fibcomputes Fibonacci numbers recursively.@lru_cachememoizes results, one computation per distinctn.- Without the cache, runtime is exponential.
- With the cache, runtime is linear in
n. maxsize=Nonemeans no eviction.- For
fib(5), the deepest calls come first and repeated arguments hit the cache. - The cache is shared across threads and processes.
Splitting the answer this way makes the weak spot easier to see. Claims 1 through 6 describe what the code does. Claim 7 is about the runtime environment, and it is wrong for processes. Each process has its own cache, because the cache lives in the memory of the Python process that created it. Threads in one process share it. The wrong sentence sits in the middle of correct ones, so skimming the paragraph for overall "feel" would not catch it.
Sort claims by how they are supported
Once you have a list, sort each claim by its source of support. The kinds of support differ in reliability.
| Kind of claim | Example from the answer above | Where to look | Typical risk |
|---|---|---|---|
| Directly visible in the code you pasted | The function returns `n` when `n < 2` | The pasted code | Low. You can confirm it by reading. |
| General library or language knowledge | `lru_cache` memoizes results | Official documentation | Medium. Details depend on version. |
| Performance or complexity claims | Linear time with the cache | A small run or measurement | Medium. Often true but stated without conditions. |
| Claims about code you did not share | "Callers pass integers only" | The real callers | High. The assistant cannot see them. |
| Claims about your environment or project | "Parallel workers reuse the cache" | Your deployment and docs | High. This is guesswork unless you described the setup. |
The last two rows are the riskiest. If the assistant describes something it was not shown, it is inferring, and the inference may be reasonable or invented. When an answer says "this is called from..." or "in your pipeline this means...", ask yourself what in the prompt supported that.
Look for these signals while reading
Specific claims you can test
A sentence such as "the cache stores one entry per distinct argument" can be tested. A sentence such as "this improves performance significantly" cannot, because it names no number and no condition. Prefer to trust, and then check, the testable ones. Treat vague ones as filler.
Claims that go beyond the code
Compare each claim against what you pasted. Assistants often add context: why the original author wrote it, what the project does, what the input looks like. Some of this is a helpful guess, but it should be labeled as one. If the answer states a guess as fact, mark it.
Version and configuration dependence
Many true statements are true only for some versions. For lru_cache, a few details matter:
@lru_cachewithout parentheses works from Python 3.8 onward. In earlier versions you must write@lru_cache().functools.cachewas added in Python 3.9 as a shorter form oflru_cache(maxsize=None).- Arguments must be hashable, so calling
fib([1])raisesTypeError.
An answer that never mentions the version, when version matters, has left something out. That is why the prompt in this tutorial's earlier chapters asks the assistant to say which parts depend on version or configuration.
Hedging that matches reality
Check whether the hedges are placed well. A good answer is firm about what the code shows and cautious about what it cannot know. Two bad patterns are common:
- Everything is stated with the same certainty, including guesses about unseen code.
- Everything is hedged ("this might possibly be...") including plain facts visible in the code.
Either pattern means the answer does not tell you where to look.
Internal consistency
Read the answer against itself. If one paragraph says the cache is unbounded and a later one says old entries are evicted, at least one is wrong. Contradictions inside a single response are a cheap, reliable signal and cost you nothing to check.
Walkthroughs that skip steps
When you ask for a step-by-step trace, compare it to the code line by line. Traces fail by skipping a branch, by miscounting calls, or by describing the behavior you would expect instead of what the code does. For fib(5) with the cache, a correct trace goes like this:
fib(5)callsfib(4), which callsfib(3), thenfib(2), thenfib(1).fib(1)returns 1 and counts as a miss.fib(0)returns 0 and also counts as a miss.fib(2)stores 1. Thenfib(3)callsfib(1), which is now a hit, and stores 2.fib(4)callsfib(2), a hit, and stores 3.fib(5)callsfib(3), a hit, and stores 5.
That gives 6 misses (one for each of 0 through 5) and 3 hits. If an assistant's trace gives different numbers, find the step where it diverges.
A reading routine
Use the same short routine each time. It takes a couple of minutes and catches most problems.
The "does it matter" step keeps the work bounded. If you are reading the function to review a pull request, the thread-safety claim may matter a great deal. If you only want a rough idea of what a helper does, you can leave it unchecked and note that you did.
Turn claims into checks
A marked claim is not yet verified. You can write the check down right away, even if you run it later. For the claims above:
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(5))
info = fib.cache_info()
print(info)
print("entries stored:", info.currsize)
try:
fib([1])
except TypeError as err:
print("TypeError:", err)This prints 5, then CacheInfo(hits=3, misses=6, maxsize=None, currsize=6), then the entry count, then TypeError: unhashable type: 'list'. The numbers match the trace above. If your output differs, either your Python version differs from what you assumed or the claim you were checking is wrong, and either result is useful to know.
For the thread and process claim, you would write a different, small test: start a second process and call fib.cache_info() there. The new process starts with an empty cache. This chapter does not run that test. Chapter-level verification methods come later in the tutorial. The point here is that you can state the check before you do it.
Ask what the answer does not say
Missing information is harder to spot than wrong information. After reading, ask what a thorough explanation of this code would normally cover that this one skipped. For a cached recursive function, a thorough answer might mention:
- The recursion depth limit. Python's default limit is about 1000 frames, so a large
ncan raiseRecursionErroreven though the cache removes the repeated work. - Memory growth. With
maxsize=None, every distinct argument stays in memory for the life of the process. - Argument requirements. Arguments must be hashable.
- Version differences, such as the
functools.cachealias.
If the answer covers none of these and the code will run on real inputs, ask about them in a follow-up. The next chapter covers how to phrase those questions.
Common reading mistakes
- Reading for the gist only. The gist is usually right, and the errors are in the details you skimmed.
- Trusting a confident tone. Confidence is a property of the writing, not evidence about the code.
- Checking only the claims you doubt. Include at least one claim that you believe, since your beliefs can be wrong too.
- Accepting claims about unseen code. If the assistant was not shown the callers, a statement about them is a guess.
- Treating one correct trace as proof for the whole answer. A correct walkthrough of
fib(5)says nothing about the thread-safety claim.
Exercise
An assistant explains a data-processing function and says: "This function is only ever called with lists of strings, so the empty-list case cannot happen in your pipeline." You pasted only the function, not its callers. Which kind of claim is this, how risky is it, and what is one way to check it?
Show answer
It is a claim about code the assistant was not shown, so it is an inference, not an observation. It is high risk, because the assistant has no evidence about the callers. To check, search the project for every call site of the function (for example with grep) and confirm what each one passes, or add a temporary assertion or log line at the top of the function to see whether an empty list ever arrives.
Exercise
Run the check script from this chapter, then call fib(30) and print fib.cache_info(). Compare the numbers with this claim from an assistant: "Because of caching, fib(30) makes about 30 calls to the underlying function." What do the numbers show, and how would you rewrite the claim?
Show answer
For fib(30), cache_info() reports hits=28, misses=31, currsize=31. The underlying function body runs once per miss, so it runs 31 times (once for each n from 0 to 30), which is roughly 30 but not exactly. A more precise claim is: "The function body runs 31 times, once per distinct argument from 0 through 30, and the other 28 calls are served from the cache."