Sub topic 4
Write a clear explanation prompt
This chapter assumes you can read basic Python, have used a chat-based or terminal assistant, and can run a small script locally. It also assumes you have already decided what you need to understand and gathered the code you want explained. Here you will put those pieces into one prompt.
Why structure matters
An assistant answers the question you actually wrote, not the one in your head. A prompt like "What does this code do?" gets a generic tour. A prompt that states your situation, your knowledge gap, and the kind of answer you want gets an explanation you can use.
The running example is a snippet that looks odd if you have not met the lru_cache decorator:
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)The four parts of a prompt
A good explanation prompt has four parts.
- Your situation. Say where the code comes from and what you are doing. "I'm reading a Python utility in a data-processing project" tells the assistant what kind of reader you are and what matters.
- The exact gap. Name what you do not know. "I don't know the
@lru_cachedecorator" is better than "I don't understand this." - The kind of explanation. Ask for a summary, a step-by-step walkthrough, or a rationale (why the code is written this way). You can ask for more than one.
- The code, clearly delimited. Put the code inside markers so the assistant can tell your instructions from the code you are asking about.
Here is a prompt that uses all four:
I'm reading a Python utility in a data-processing project and I don't know the @lru_cache decorator.
Explain what this function does and why the decorator is there. Walk through fib(5) step by step. Tell me which parts you are certain about and which depend on Python version or configuration.
<code>
from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
</code>Why each sentence is there
The first sentence gives the situation and the gap. The assistant can now skip an introduction to Python and focus on the decorator.
"Explain what this function does and why the decorator is there" asks for two things: behavior (a summary) and rationale. Without the second request, you might learn what fib returns but not why someone added caching.
"Walk through fib(5) step by step" asks for a trace on a concrete input. A trace is easier to check than a general description, because you can reproduce it yourself.
"Tell me which parts you are certain about and which depend on Python version or configuration" asks the assistant to separate firm claims from conditional ones. For example, maxsize=None means the cache has no size limit, which is a different behavior from the default of 128 entries. Asking for this split tells you where to look first when you verify the answer.
The <code> tags are a delimiter. Triple backticks or any other clear marker work too. What matters is that the boundary is unambiguous, especially when the code contains comments or strings that read like instructions.
What the structure looks like
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| "Explain this" with no gap named | A generic answer that repeats what you already know | Name the construct or idea you don't know |
| No kind of explanation requested | A summary when you needed a trace, or the reverse | Ask for a summary, walkthrough, or rationale |
| Code pasted with no delimiter | The assistant may mix the code with your instructions | Wrap the code in tags or backticks |
| Several unrelated questions in one prompt | Shallow answers to each | Ask one focused question, then follow up |
| No request to mark uncertainty | Guesses and facts sound equally confident | Ask which claims depend on version or configuration |
Check the answer against the code
The prompt asks the assistant to say which claims depend on configuration. You can test the main claim, that results are cached, by running the snippet and inspecting the cache:
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())cache_info() reports hits, misses, and cache size. For fib(30) it shows 31 misses (one for each value from 0 to 30) and 28 hits. If an assistant claimed the function recomputes every call, or gave different numbers, this output would show it. Later chapters cover verification in more depth. For now, notice that a prompt that asks for a concrete trace gives you something you can compare to real output.
Exercises
Exercise
Rewrite this prompt so it has all four parts: "What does this do? @property def total(self): return sum(i.price for i in self.items)". Assume you are reading a shopping-cart class in a Python web app and you have not used @property before.
Show answer
Example: "I'm reading a shopping-cart class in a Python web app and I don't know what @property does. Explain what the total method does and why it is decorated this way. Walk through a call to cart.total with two items priced 5 and 7. Tell me which parts you are certain about. <code>@property\ndef total(self):\n return sum(i.price for i in self.items)</code>" It should give the situation, name the gap (@property), request a summary, rationale, and a trace, and delimit the code.
Exercise
Why does the example prompt ask the assistant to say which parts depend on Python version or configuration, and how does that help you?
Show answer
It makes the assistant separate firm claims from conditional ones, such as the effect of maxsize=None versus the default cache size. You then know which claims to check first in the documentation or by running the code.