Placing breakpoints

Breakpoints, as the term suggests, are markers to be placed in our programs to indicate places where the execution in a debugging session should pause. While a program being debugged is paused, developers then can examine that frozen state of the program as well as its variables, with the goal of gaining insight into how those variables are mutated at a specific step of the program. We will see that this is the exact tool we need to debug our example program.

Breakpoints are to be placed at specific lines of code in our program. To insert a breakpoint in PyCharm's editor, click on the region between the line numbers and the actual code, called the gutter, at a line where you'd like to place a breakpoint. Once placed, PyCharm's breakpoints are represented as red circles. In our current example, let's place breakpoints at lines 3, 5, and 12, as shown here:

Placing breakpoints in PyCharm

The general strategy of using breakpoints is to freeze the program after each time the variables we are interested in are mutated. Again, we are trying to see what happens when the a variable is changed inside the change_middle() function, and why the b variable is also changed. For this reason, we have the following breakpoints:

  • The breakpoint at line 12 pauses the program before the function is called
  • The ones at lines 3 and 5 help us inspect the program right before and after the mutation of the a variable at line 4

Now, let's start our debugging session with these breakpoints added. Slowly, click on the resume button to step through the program again, but stop when you have reached the frame that displays <module>, main.py:12. This is the first breakpoint that our program reaches (at line 12):

Walking through a program in debugging mode

There are a number of elements that we should notice:

  • The line highlighted in blue is where the execution is currently at. Again in our example, it should be at line 12.
  • The Variables section in the Debug panel: At the current frame, we can see that this section displays the corresponding variables, which are a and b. We can see that they both hold the same value—a Python list, [0, 1, 2], as shown here:
Variables at breakpoints

Notice that you can also expand each variable in this section to further inspect the individual elements in the list.

  • Inline debugging: Information regarding the variables of our program is also displayed inside the editor. For example, at this point, you will notice the following at lines 9 and 10 of your editor:

Inline debugging in PyCharm

These comment-like codes are automatically inserted into your editor to display the current variable values at each specific breakpoint. After you have fully examined your program at a breakpoint and want to move on with the execution, you can use the resume button again.

After clicking the resume button once, the current execution should move into the change_middle() function and jump to line 3 (which is our second breakpoint). Let's keep our debugging session paused at this point to discuss other debugging features in PyCharm—specifically the various stepping functionalities in the next subsection.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.19.251