Debugging with pdb
Most of the Python developers reading this book might already be aware of the usage of pdb, that is, the Python debugger. For those who are not aware of it, pdb is an interactive source code debugger for Python programs. We can set breakpoints wherever needed, debug using single-stepping at the source line level, and inspect the stack frames.
Many new developers might be of the opinion that the job of a debugger can be handled using a logger, but debuggers provide a much deeper insight into the flow of control and preserve the state at each step, and hence, save a lot of development time.
Getting ready
We will use Python's built-in pdb module for this recipe and use it in our application from the last recipe.
How to do it…
Using pdb is pretty simple in most cases. We just need to insert the following statement wherever we want to insert a breakpoint to inspect a certain block of code:
import pdb; pdb.set_trace()
This will trigger the application to break execution at this point...