Sub topic 6
Read the answer critically
This chapter assumes you can read basic Python, have used a chat or editor assistant, and can run small snippets locally. It covers how to read an assistant's explanation of code so you can separate what is solid from what needs checking.
Why an explanation needs reading, not just accepting
An assistant writes fluent text whether or not it is right. A wrong claim sounds the same as a correct one. Your job after the answer arrives is to sort its claims into three groups:
- Claims you can see directly in the code you pasted.
- Claims about behavior that depend on a language version, a library, or configuration.
- Claims about code the assistant never saw.
The running example is the fib function from earlier chapters.
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)Sort the claims
Suppose the assistant replies: "This computes Fibonacci numbers. The decorator caches results, so each value is computed once. This makes the function run in linear time. It also uses little memory, and it works the same on every Python version."
Split that into separate statements and mark each one.
| Claim | Type | Action |
|---|---|---|
| It computes Fibonacci numbers | Visible in the code | Read the base case and recursion |
| The decorator caches results | Library behavior | Confirm in the `functools` docs or with `cache_info()` |
| Each value is computed once | Follows from caching | Run it and count misses |
| Linear time | Reasoning about the cache | Check the miss count against `n` |
| Uses little memory | Doubtful | `maxsize=None` means the cache never evicts, and deep recursion also uses stack |
| Same on every version | Unsupported | Check the docs for version notes |
The last two claims deserve the most suspicion. Words like "always", "never", "little", and "the same everywhere" are often where an answer outruns its evidence.
Look for these signals
Read the answer with a few questions in mind.
- Does it match your code? Check that names, parameters, and the order of operations match what you pasted. Assistants sometimes describe a more common version of a pattern instead of your version.
- Does it say how sure it is? If your prompt asked for certainty levels, look at what it marked as uncertain. If it marked nothing, that is not evidence that nothing is uncertain.
- Is it specific? "Caches results for repeated calls" is checkable. "Optimizes performance" is not.
- Does it mention things you never provided? References to a config file, a caller, or a helper function you did not paste are guesses unless it says where they came from.
- Does the walkthrough agree with itself? If it traces
fib(5), count the steps. Numbers in a trace should be consistent with the code.
A worked check of a trace
Say the assistant claims that fib(5) makes calls to fib(4), fib(3), fib(2), fib(1), and fib(0), and that after the first call, fib(3) is served from the cache. Walk through it yourself.
fib(5)callsfib(4)first.fib(4)callsfib(3), which callsfib(2), which callsfib(1)andfib(0).- The right-hand call in
fib(3)isfib(1), which has already been computed, so it is a hit. - Back in
fib(4), the call tofib(2)is a hit. - Back in
fib(5), the call tofib(3)is a hit.
So the claim is mostly right, with one correction: fib(3) is served from the cache only when fib(5) asks for it a second time, not "after the first call" in general. Small timing errors like this are common. They matter when you later rely on the explanation to reason about the code.
Turn doubts into checks
For every claim you marked as "check", write down how you would test it. Use the lightest method that settles 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())Running this prints 832040 and a line reporting 31 misses and 28 hits. That agrees with "each value is computed once": there is one miss for each of the 31 inputs from 0 to 30. If the assistant had said 30 misses, you would now know it was off by one.
Common errors when reading answers
- Treating a correct summary as proof that the details are also correct.
- Skipping the parts of the answer that are hard to follow, since those often hide the errors.
- Accepting version-specific statements without checking which version you run.
- Trusting a claim about code the assistant has not seen.
Later chapters cover follow-up questions and independent verification in more detail. Here the goal is only to mark what needs attention.
Exercise
An assistant says your cached function "uses almost no memory and works on any input size." Which part of the code makes this claim doubtful, and how could you test it?
Show answer
With maxsize=None the cache never discards entries, so memory grows with each distinct input. Recursive calls also add to the call stack, so a very large n can raise RecursionError. Test it by calling fib on increasing values of n, checking fib.cache_info().currsize, and watching for RecursionError.
Exercise
Split this sentence into separate claims and label each as visible in the code, dependent on library behavior, or unsupported: "The decorator makes fib thread-safe and fast."
Show answer
"Fast" is a library-behavior claim that you can measure by timing calls. "Thread-safe" is unsupported as stated. The functools documentation describes what lru_cache guarantees, and you should read it before relying on any thread-safety claim.