To practice interacting with pdb, you've written a short script:
a = 0
b = 42
breakpoint()
a += b
breakpoint()
After setting a breakpoint() in your code and executing your script, you'll be launched into a debugger session with Python's default debugger, pdb. You'll be greeted by a prompt:
(venv) ➜ python debugging.py
> /Users/martin/Documents/codingnomads/debugging.py(4)<module>()
-> a += b
(Pdb)
You'll find a highlighted prompt next to (Pdb). When you see this, then you're right inside an interactive debugging session.
Explore The Debugger
As mentioned earlier, the interactive debugger session gives you the opportunity to explore the current state of your program. You can:
- inspect the value of all variables that are currently defined
- interact with these variables beyond just printing them
With this in mind, go ahead and explore the current state of your script.
Debugger Practice
- What is the output of
print(a)if you run it right after starting the debugger? - Can you also inspect the value of
b? - What happens if you execute
print(b + 3)in the debugger? - What is the output of
print(a)at the secondbreakpoint()? - Can you change the value of one of your variables using an assignment as you would inside your script?
Info: you can step forward in your code execution by typing next or n and pressing Enter.
Going through these tasks shows you some of the possibilities and some limitations of the interactive debugger. But while you'll run into errors when trying to assign a new variable or change the value of an existing variable in the way you're used to, this is actually possible, and you'll learn how to do it in the next lesson. You'll also get to know the most important commands and shortcuts for interacting with pdb.
Info: It's possible to see the code written further up, but you can't see any previous states of your variables.
In a future lesson, you'll get to know the most important commands for interacting with pdb in an interactive debugger session.
Additional Resources
- DigitalOcean: How to use the Python debugger
Summary: What is Python PDB
- Interactive debugging is the practice of inspecting your code to uncover potential errors. It consists of the following broad steps:
- Declare specific points in your script where you want to inspect the current state of your program. These are called breakpoints, and you can set them with
breakpoint(). - Execute your code as you normally would. This will start an interactive debugger session at the first
breakpoint(). - Explore the current state of your program.
- Step to the next
breakpoint()and continue exploring.
- Declare specific points in your script where you want to inspect the current state of your program. These are called breakpoints, and you can set them with
- One limitation of a basic debugger is that you can only go forward. Stepping backward through your code doesn't work because the debugger doesn't keep a memory of the previous states of your program.
- Over the years, a couple of new libraries have come out that improve on
pdb's basic workflow, but sincepdbcomes with Python's standard library, it's still a popular debugging tool that you should know how to use.