https://www.physicsforums.com/insights/wp-content/uploads/2015/11/pythondebug2.png 135 240 Mark44 https://www.physicsforums.com/insights/wp-content/uploads/2016/08/PF_Insightsnew.png Mark442015-11-26 07:27:392017-04-08 02:09:52Simple Python Debugging with Pdb: Part 2
This Insight article is the continuation of the first article, Simple Python Debugging with Pdb: Part 1.
In this article, let’s look at another important capability of debuggers: breakpoints. When you set a breakpoint in a program, the debugger executes all of the code up to the breakpoint, and then halts. This allows you to inspect variables at that point in the program.
To set a breakpoint, use the Pdb command b or break, followed by the line where you want the breakpoint. It’s helpful to press l (lowercase L) or ll to list (or longlist) your program code with line numbers, as I have done in the following screenshot. The l and ll commands were discussed in the previous Insight article. In this screenshot, the command b 8 means that I’m setting a breakpoint at line 8.
Instead of single-stepping through the code using n or s (next or step) as I did in the previous article, we can use the c (or cont or continue) command to execute code until we reach a breakpoint. The next screenshot shows the result of pressing c — execution stops at the line with y = fun(x) on line 8, which is where I set the breakpoint.
Pressing c again causes the print() statement to execute, and then starts another iteration of the for loop. Execution stops for the second time at line 8.
You can also set a breakpoint to be triggered conditionally, by adding a logical condition after the line number. I’m going to clear (cl or clear) the breakpoint I set at line 8, using the command cl 1. This was the first breakpoint I set, so its number is 1. Then I’m going to set a new breakpoint inside the function fun(), but only after the expression to be returned is larger than 20.
To do this I use the command b 4, 3 * x + 1 > 20. This command means, “set a breakpoint at line 4, and trigger it when 3 * x + 1 is larger than 20.” Note that you must use a comma between the line number (4) and the logical expression 3 * x + 1 > 20 .
After setting the breakpoint and the logical condition, I’ll press c to continue execution until the breakpoint is hit. As you can see in the following screenshot, my program has processed x values of 2, 3, 4, 5, and 6, and has stopped inside the fun() function when x is 7, and 3 * x + 1 is 21, the first value that was larger than 20.
- Set a breakpoint by typing b or break, followed by the line number at which to set the breakpoint.
- Clear a breakpoint by typing cl or clear, and the number (not the line number) of the breakpoint.
- Set a conditional breakpoint by typing b (or break), following by the line number, followed by a comma, and then the logical condition.
- As already mentioned, typing h (or help) at the Pdb prompt lists all of the commands, and typing h followed by a specific command gets you help for that command.