In this video, learn how to set breakpoints in order to step through and debug your code. Setting breakpoints is a great way to understand the flow of your code, especially when using object-oriented programming with inheritance.
- [Narrator] Hello, and welcome to Python Projects. In this video, we will be setting breakpoints in order to debug our code and understand it better. First, we define a class, PythonProjects. Down here, we create an instance of the class, and what's in the class is init method. We call the print message method, and we now run it. It'll print out the print statement we passed in to the Python console.
Next, we add a second class to our module, and within the first class, we create an instance of the Utilities class, and on this instance, recall the print message was timestamp method and within this method, we are adding today, and today we got by importing the datetime module, calling now on it and then formatting the string time for year, month, and day. We may now execute the program. We get our original message, and we get the second message, which we called over here, which has the at sign, plus today's date added to it.
Next, we're going to use inheritance. So, in our second class on the Utilities class, we inherit from the first class, the PythonProjects class, and we use the same method name, print message, which overrides the original print message from the base class. When we now execute our code, first, we create an instance of the first class, then we create an instance of the second class, which inherits from the first class, and we print out the message.
When we now run it, we get the following result. We get our original message, we get the message with the datetime stamp, and we get a second message with the datetime stamp. So, this might not quite be what we had expected, so in order to figure out what's going on, we're going to set some breakpoints and step through our code. In the Eclipse IDE, with the PyDev plugin, setting a breakpoint is very easy. All we have to do is click on the left side here, double click it, until a green pin mark appears, and now when we run the program, we can step through by clicking on the debug button and the first time we do that, it will bring us this pop-up asking us if we want to use the debug perspective, which is what we want to do, so we click yes here.
The debug perspective opens up several windows. On the left top side is the call stack window, on the right is where we can see breakpoints at local variables, and there's also an outline. So, we don't need all of those windows all of the time, so we can just close some of them. The way to get them back, if you're interested in them, is to go to Window, Show View, and reselect the view we just closed. Now, we also can move the windows around a little bit so we have more space, we can see what really interests us.
With the breakpoint set, we can now step through our code. We do this by clicking on this button, step enter, and then we can step over. This will print the message, and we go just one step further, step into again, and now we're in the Utilities class. In the previous moment, we were at the PythonProjects base class, but now we're actually in the Utilities class and because the constructor already called print message once, now we're calling it for the second time and that's why we end up having two of the same print segments printed out.
So, what our breakpoint showed us is that we don't need that extra print statement here because we're already calling it, because we inherit from the base class, when we call the self print message statement for the first time, it actually executes the Utilities message, which prints out the date string. So, what we can do is we can just remove this extra call and run our program again. Now, what we get is what we had intended we'd get.
The message and a second message with the datetime of today.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Running Python programs within Eclipse
- Setting breakpoints
- Using the PyDev debugger
- Using design patterns
- Creating a GUI application with Tkinter
- Writing a Windows scheduling service