Simple Python Debugging with Pdb: Part 2

[Total: 1    Average: 5/5]
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.
In summary:
  1. Set a breakpoint by typing b or break, followed by the line number at which to set the breakpoint.
  2. Clear a breakpoint by typing cl or clear, and the number (not the line number) of the breakpoint.
  3. Set a conditional breakpoint by typing b (or break), following by the line number, followed by a comma, and then the logical condition.
  4. 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.

Click For Forums Comments

Former college mathematics professor for 19 years; taught a variety of programming languages. Former technical writer for 15 years at a large software firm headquartered in Redmond, WA.
I enjoy traipsing around off-trail in Olympic National Park, as well as riding and tinkering with my four motorcycles.

2 replies
  1. Mark44
    Mark44 says:

    Mark, how would you compare Python with other similar languages?

    Python is in some respects similar to C, but is a language that is significantly higher in level. Without using any external libraries/modules, you can get permutations and combinations very quickly.

    A couple of the features of Python that distinguish it from C and the languages that derive from C are the map() function and list comprehension. map() returns an iterator that will apply some operator to one or more lists (depending on the operator). In the example below, corresponding pairs of numbers are multiplied in two lists to form a new list that contains these products, and then the sum() function is applied to the list to add all of the numbers. In short, the one-line body of the dotprod() function calculates the dot product of the two lists that are in its argument list. Lower-level languages such as C will typically use a for loop to iterate through the lists.

    [code=python]# — find the dot product of two vectors
    import operator

    def dotprod(u, v):
    return sum(map(operator.mul, u, v))

    u = [1, 2, -1, 4, 2, 1, -2, 6]
    v = [1, 4, -1, 0, 1, 1, -2, 4]

    ans = dotprod(u, v)
    print(“Result is: “, ans)[/code]
    This code displays 41 as its result.

    List comprehension is “A compact way to process all or part of the elements in a sequence and return a list with the results.”
    Here’s an example that works with the list [0, 1, …, 255] and creates a new list with only the list elements that are evenly divisible by 16.
    [code=python] — simple example of list comprehension
    my_list = [x for x in range(256) if x % 16 == 0]
    The print() statement displays the list as [0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240]

    I’m sure there are quite a few more differences, but these are just a few that come to mind.

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply