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.
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
j = i * 2, which corresponds to the highlighted line in your source code.
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
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.
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.
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: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.
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.
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
j, and the Locals panel is empty again.
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.
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.
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.
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?
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.
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.
Become a Member to join the conversation.