Code Explainer and Walkthrough
Added Sep 29, 2026
About This Prompt
This prompt has ChatGPT explain unfamiliar code at your experience level before you change, review, or reuse it. It works through the code in a fixed order: a short summary, a block-by-block walkthrough that explains why each part is written the way it is, a trace of one realistic input with the key variable values at each step, a glossary of language features you may not recognize, and the non-obvious behavior such as edge cases, side effects, and assumptions about inputs. It ends by pointing to the parts that matter most for what you are about to do. It suits developers inheriting legacy code, reviewing a teammate's pull request, or learning a language from real code, and it tells ChatGPT to state its assumptions about anything it cannot see rather than guess.
Variables to Customize
[LANGUAGE]
The programming language, plus the framework or library if one is involved
Example: Python
[CODE]
The code you need explained, including its imports and any helper functions it calls
Example: def retry(times=3, delay=1.0): def wrap(fn): @functools.wraps(fn) def inner(*args, **kwargs): for attempt in range(times): try: return fn(*args, **kwargs) except Exception: if attempt == times - 1: raise time.sleep(delay * 2 ** attempt) return inner return wrap
[READER_LEVEL]
Your experience with this language and this kind of code
Example: a junior developer who knows basic Python but has never written a decorator
[GOAL]
What you are about to do with the code
Example: I need to add a maximum total wait time without breaking the functions that already use it
Tips for Best Results
- Say what you are about to do with the code, whether that is fixing a bug, reviewing a pull request, or extending it, so the explanation spends its words on the parts that matter for that job.
- Include the imports and any helper functions the code calls. Without them ChatGPT has to assume what they do, and the prompt makes it tell you when it is assuming.
- If the trace uses an input your system never sees, reply with a real one and ask ChatGPT to trace it again.
Example Output
**Summary:** `retry` is a decorator factory. It wraps a function so that, if the function raises an exception, it is called again up to `times` attempts in total, waiting longer before each new attempt. It is typically used on network or database calls that fail intermittently. **Walkthrough:** - `retry(times, delay)` does not touch your function. It returns `wrap`, which is the actual decorator. That is why it is applied as `@retry()`, with parentheses. - `wrap(fn)` builds `inner`, the function your callers really call. `functools.wraps` copies `fn`'s name and docstring onto it, so logs and tracebacks still show the original name. - `inner` calls `fn` inside a loop. A successful call returns immediately. A failure on the last attempt is re-raised by `raise`. Any other failure sleeps, then tries again. **Trace:** `@retry()` on `fetch_prices()`, which times out twice and then succeeds: | attempt | result | action | |---|---|---| | 0 | raises TimeoutError | sleep 1.0 × 2^0 = 1s | | 1 | raises TimeoutError | sleep 1.0 × 2^1 = 2s | | 2 | returns data | return it | Total wait: 3 seconds. In general the worst-case total is delay × (2^(times−1) − 1). **Non-obvious behavior:** - `except Exception` retries every error, including ones that can never succeed on a second try, such as a ValueError caused by bad input. - `@retry` without parentheses passes your function in as `times`, so the decorated name no longer behaves like your function. - `times=0` never calls your function at all and returns None. **For your change:** check the time budget just before `time.sleep`, and add it as a keyword argument that defaults to None so existing `@retry()` callers keep today's behavior.
Quick Info
- Tool
- ChatGPT
- Category
- Coding
- Difficulty
- beginner
- Variables
- 4
- Type
- text