How can I trace which function call produced an unexpected result?
Quick answer
Start at the line that produces the result, then trace back through the functions that returned it. For each call, note the call site, the values passed, the parameters those values bind to, and the returned value. Python gives each call its own local symbol table, so keep separate notes for nested calls. Compare the trace with what you expected; the first step where they differ is a useful place to investigate.
The question, the details, the next step.
A practical checklist
Start from the statement where the unexpected result appears.
Copy each relevant call site exactly and note its execution order.
Map each argument to its parameter, including keyword names.
Give nested or repeated calls separate entries and record each return.
Compare observed and expected values to find the earliest mismatch.
A few more useful answers
Should I trace from the first function in the file?
Usually, start at the result you are investigating and follow its contributing calls back to their call sites. That keeps the trace focused on the value in question.
Does inspecting a function signature tell me what values a past call received?
No. A signature can help check a callable’s accepted parameters, when introspection is available, but it does not reveal the actual arguments or return value from a particular past call. [S2]
What if I cannot tell whether a value is wrong or merely unexpected?
Write down the expected value and why you expect it, using the relevant requirements or inputs. A trace shows where the observed result diverges; it cannot decide the intended behavior for you.
Sources
References used for this guide.
- [S1] Python Tutorial: More Control Flow Tools — Function calls and arguments docs.python.org · Checked
- [S2] inspect — Inspect live objects — Python 3.15.2 documentation docs.python.org · Checked
