The Step, Out, and Over Buttons
00:00 In the previous lesson, you got a high-level overview of the UI (user interface) features of IDLE’s Debug Control window. In this lesson, you’ll look at the Step, Out, and Over buttons and see what they do when you step through your code. Now, after you’ve started the debugger on this short script, you can go ahead and press the Step button.
00:24
You see that some things changed. First of all, inside of your source code, you can see that now line 2 is highlighted. Also, in the Stack panel of the Debug Control window, you can see that the highlighted line now reads line 2:
j = i * 2
, which corresponds to the highlighted line in your source code.
00:49
Most importantly, however, you can see that in the Globals panel at the bottom of your Debug Control window, you now have a new variable that popped up, the variable i
, and it points to the value 1
.
01:03
This is here because your for
loop in line 1 created the variable i
and assigned to it the value 1
.
01:12 You might already notice how beneficial this can be for debugging if you can see the changing value and the changing variables in your code, just as you step through it. Now, just as before, the highlighted line that you can see in the Stack panel and highlighted in your source code, as well, has not executed yet. The debugger again stopped just before executing this line.
01:38 If you press Step once again, then line 2 is going to execute, and the debugger is going to stop again at line 3. So go ahead and press Step, and you can see the Stack changed.
01:51
It highlights a different line of code. And in the Globals panel, you can see additionally to i
, you also have the variable j
that now points to the value 2
.
02:04
Now, if you continue to press Step next, you might expect that print()
is just going to display the message in your console, and that’s it. But instead, if you press Step, you can see a new line being highlighted that says 'idlelib.run'.write(), line 463: if self.closed:
.
02:26 And if you look at the Globals panel, it’s suddenly populated with a lot of information and a lot of variables that you’ve never seen before. Also, you might notice that the Locals panel now has a string that looks familiar.
02:42
The string is called s
and it says 'i is 1 and j is 2'
. This is what you expect to be printed to the interactive window, but it hasn’t shown up there yet.
02:55 So what is happening? If you still have the Source checkbox checked at the top of the Debug Control window, you will see that there’s also an additional file that opened up.
03:06
This file is called run.py
, and currently you can see that line 463 is highlighted, just like the message in the Stack trace told you. The reason why you see this is that if you use Step to step through your code, it will also step inside of built-in Python functions. In this case, you’ve stepped into the execution of the print()
function that is part of your script on line 3.
03:35 Now there’s a whole lot of code that this is going to go through if you continue pressing Step. Go ahead and try a couple of times.
03:44 You can see it just keeps jumping through Python internal code, and this is not very helpful for your debugging session, because most likely the bug is somewhere in the code that you wrote and not inside of Python itself.
03:59
So what you can do to get out of the situation is you want to press the Out button. The Out button finishes the execution of a current scope and takes you back out of it. In this case, this will take you out of the internals of the print()
function and back into your script. So go ahead and press the Out button.
04:21
You might have to press it more than once because of the internals that you dug into. But at some point you will see that you’re back to the more-familiar setting where your Globals is populated with the couple of built-in variables that you’ve seen before, as well as i
and j
, and the Locals panel is empty again.
04:43
Go ahead and close this window, run.py
, so that you’re back to the start. And you can also see that for i in range(1, 4):
is highlighted again, and this is also the line that your debugger tells here is going to be executed next. Finally, you can see that in your interactive window, the string i is 1 and j is 2
has printed out finally.
05:11 So as a quick mid-recap, the Out button is helpful if you accidentally or purposefully stepped inside of a function, but you don’t want to actually step through everything that’s happening in there, and you want to get back out. That’s what you would use the Out button for.
05:27
Now you can continue to step through the code. Again, you will see the variables change in the Globals panel and the highlights change in your script, as well as the message inside of the Stack panel. And now you’re—if you step once more, you can see that the next line that will be executed is again the print()
function on line 3 of your script.
05:52
Now this time, you want to avoid stepping into this internal print()
function’s code, and you can do that by pressing the Over button.
05:59
The Over button will skip the internals of the print()
function and just go to the next line, which is what you might have expected by pressing the Step button last time.
06:10 So we’ll go ahead and press Over, and you can see that what you expect to happen: you can see the print-out in the interactive window, you can see you’re still inside of your script, the Globals and the Locals make sense, and also in your source code, the next line that’s highlighted is the next iteration of your loop on line 1.
06:33
Now continue pressing Step two more times, watch how the variables change in the Globals window, and then once you reach the print()
function again, what do you think will happen when you press Over one more time?
06:49 So I go ahead and press Over, and you can see the message printed out, and the debugger is ready to execute line 1 again. If you click Step,
07:00
however, the debugger quits. You can see that by the buttons being grayed out and the [DEBUG ON]
string appearing in your interactive window.
07:11
And this happens because your script has reached the end. The for
loop ran three times and printed out the three messages, and your script has arrived at the end. And if that’s the case, the debugger simply becomes unresponsive.
07:27 And if you wanted to run it again, you’d have to start running the script from the beginning, pressing F5 again. Now this completes a walkthrough of stepping through your code using Step, Out, and Over.
07:41 You use Step to go from one line to the next line and/or stop execution before the next line so you can investigate the currently defined variables.
07:52
And you also learned about using Out to get back out of a function instead of needing to step through every line of, for example, the built-in print()
function. And finally, you also learned about Over that you can use to avoid stepping into a function, but instead just complete its execution and take you to the next line in your script. Now, there are two more buttons and there’s breakpoints, and you will learn about Go, Quit, and setting a breakpoint and what that means in the next lesson.
Corbin Nexus on Jan. 21, 2024
Hi!
I’ve been using the application Thonny to debug my code when I try to figure out what is happening (or not happening). I think it shows it a bit clearer than the IDLE Shell. But it’s good to be familiar with both =)
Martin Breuss RP Team on Jan. 22, 2024
Good choice @Corbin Nexus, we also have a full tutorial focused on using Thonny as your IDE.
We’re sticking with IDLE throughout the Python Basics series to give learners a straightforward learning experience without getting lost in the sea of possible tooling. However, if anyone is curious, there are a lot of different editors and IDEs out there to try.
Shubham Prasad on Aug. 25, 2024
Hi is this way different from debugger present in vs code and pycharm. are there any tutorial available for the same.
Martin Breuss RP Team on Aug. 26, 2024
@Shubham Prasad it’s somewhat different, as these IDEs have more functionality built-in. But the basic concepts are the same.
We do have tutorials on PyCharm and VS Code that also cover some aspects of debugging. The one on debugging in our advanced VS Code tutorial is probably the most in-depth of them.
Become a Member to join the conversation.
JEX on July 18, 2023
I was surprised, that print() function on debug uses so many internal logic. Thanks.