liveBook · Manning

Debugging

As explained in Python How-To

Debugging is the process of locating, diagnosing, and correcting defects in software.

Debugging locates, diagnoses, and corrects software defects. Work through problems during development, preferably bit by bit, rather than waiting until delivery. section 13.0 section 13.2

Use progressively closer inspection to find and understand a defect:

  • Read the complete traceback, especially the exception, method name, and line number, to locate the first useful failure point. section 13.1
  • Set a breakpoint and run the program interactively, stepping through operations line by line instead of relying only on a crashing exception. section 13.2
  • Inspect pertinent values with the debugger's p command, or call locals() to view the function's complete local namespace. section 13.2.4
  • Prefer named functions over assigned lambda functions when errors need clear locations; named functions produce more informative traceback names. section 7.1.3

Locate an attribute error

Calling task.update_urgency(4) raises AttributeError because the method calls update_db(), while the class defines _update_db(). The traceback identifies update_urgency and its failing line, narrowing the correction. section 13.1

Figure 13.4 The general aspects of debugging programs in Python. For a program, we add breakpoints to the places where we debug. When the execution encounters the breakpoint, it activates the interactive debugger. Then we can perform a variety of debugging tasks, such as running code line by line.
CH13_F04_Cui

Follow the breakpoint workflow: pause execution, activate the interactive debugger, and run code line by line. section 13.2

How Python How-To covers it

How deep it goes
Moves from reading tracebacks to breakpoints, stepping, and variable inspection, while treating testing as a related activity.
Examples
Uses an AttributeError traceback, breakpoint stepping, malformed task data, and lambda-versus-named-function KeyError messages.
What it assumes
Assumes functions and local namespaces while teaching traceback reading and interactive debugging techniques.
What you can do after
Diagnose/debug with tracebacks, breakpoints, stepping, debugger expressions, and local-variable inspection.
How it connects
Connects tracebacks, exceptions, the interactive debugger, breakpoints, variables, functions, lambda functions, and testing.

Also explained in

Compare how 16 books explain debugging →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.