Visual Studio Code for Python Developers Transcripts
Chapter: Running and Debugging
Lecture: Diving into Breakpoints

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, let's take a second to talk about breakpoints and explore some of the interesting options that Visual Studio Code
0:08 offers to make working with them a little bit nicer. As you saw in a previous video, we can use breakpoints to pause the execution of
0:16 our debugging session at any point in our program where we want to expect and
0:20 see the state of any of the variables or functions we have inside of our application. Here, I'm going to set a breakpoint inside of the status router.
0:28 What I'm going to do now is I'm going to open up that sample.actp file, and I'm going to make a request to status.
0:35 As you can see, the breakpoint stopped before running the current line that I set the breakpoint at. But at this point, I can check and see
0:43 the state of any of those variables that are inside of here. For instance, I can look at the status code, and I can see, well,
0:49 it's actually not set to anything yet. But once I step over it, notice my application is still paused, and I can check and see now this is set to 200.
0:57 Right? I can go ahead and keep my application running. Now, not only does breakpoints allow me to pause the execution of my function,
1:05 but inside of Visual Studio Code, we actually have a breakpoints view. So inside of the debug section, I can open up the breakpoints view,
1:13 and now I can see all the breakpoints that are set. I can even click on those breakpoint listings,
1:19 and it'll take me to the exact file and line where that breakpoint was placed.
1:23 Within the same view, I have the option to edit any of these breakpoints, or I can even remove them if I don't want to use them anymore.
1:31 I can add new breakpoints using this plus button, or I can toggle on or off those active breakpoints for my current debugging session.
1:39 Now, while that seems pretty straightforward, there's a lot of other interesting things that we can do as well.
1:44 I'm going to set this breakpoint back to where it was before. Now, on this breakpoint, I'm going to right-click on it, and if we zoom in a little bit,
1:52 you can notice that I have an edit breakpoint option. When I click on edit breakpoint, notice now, I can set what's called a conditional breakpoint.
2:00 Conditional breakpoints allow me to pause the execution when a particular condition is met. As an example here, I can do it based on a hit count.
2:10 A hit count would be, hey, if this thing hits this particular breakpoint n number of times, maybe it's four times or five times or six times,
2:19 at that point, then I want you to pause it, or I want you to pause based on the value of an expression. Let's take a look at the expression one.
2:28 I'm going to say i is equal to 10. I'm just going to make up an arbitrary variable and value. I'm going to hit enter. Notice there's no i here.
2:37 I'm just going to set i is equal to 10 here at the top. Oops, that's my mistake. This should be i single equals to 10.
2:44 What that should be able to do now is if I go ahead and start my debugging session again. Remember, I set this to break only when i is equal to 10.
2:53 I'm going to run this request. I notice the breakpoint is stopping because, well, condition i is equal to 10, obviously.
3:00 Let's go ahead and I'm going to change that to 11. I'll restart my debugging session. But now if I go ahead and I execute this status request,
3:11 it should not break at all because, again, even though my breakpoint is set, it's not going to meet the condition because i is no longer equal to 10.
3:20 Obviously, this is a very simple use case. You can change it for whatever condition you want. Maybe you want to set a condition where the
3:26 incoming request is equal to a certain value and you want to stop at that point.
3:30 Or whatever other variable is in scope, you might want to break whenever that condition is true or not. Now, let's go ahead and stop this.
3:37 Let's take a look at the hit count one. So let's say maybe, no, I don't want to remove it. I can right click. I want to edit this breakpoint.
3:44 I'm going to change this now to hit count. And let's say I want this, you know, every time the hit count is three, right?
3:52 So when it reaches three, I want it to hit or I want it to stop at this breakpoint.
3:56 So starting the debug session again so we can see what happens here. Now my application should be running. I'm going to head back over to sample.http.
4:06 Now I'll issue my request for status. One, didn't stop. Two, didn't stop. Three, ding, ding, ding, ding, ding. We got a winner.
4:15 So notice, again, our condition was I needed to stop when the hit count is equal to three. So now notice that it stops here.
4:23 Now I can stop my debugging session. I can remove my debug breakpoint from here.
4:29 Or I can even come inside of the debug window inside of the debug view. And I can exit out from here if I wanted to.
4:37 But now you can see some of the different options you have when it comes to working with breakpoints inside of Visual Studio Code
4:43 and how you could use conditional breakpointing to set the specific conditions you want to inspect in your debugging session.


Talk Python's Mastodon Michael Kennedy's Mastodon