Posted by & filed under Python, Web development.

It’s no secret that we’re Python advocates and believe its cutthroat indentation rules are part of what makes it a great language. We use it to serve our web interface and API and the majority if our maintenance scripts are Python as well. Beginners will find it simple to read and understand. Most commands work just the way you expect them to and when things go wrong, it’s also fairly simple to figure out why. Drop a few print statements throughout your program and see where things go awry.

Print statements can be a quick way to see how far a program’s execution gets before an exception occurs or peek inside a variable, but they require some serious trial and error, re-running your program over and over to review the output — combine this with time-consuming test cases and you can quickly hit a wall.

Luckily, we have a largely underestimated tool at our disposal that can seriously reduce debugging time: the python debugger, PDB.

Using PDB gives you an almost the same benefits of the CLI, inside your program. Here’s a quick run through:

obj_name = self.handle_intersection(first_set, second_set)

import pdb

As soon as your program reaches set_trace() you’ll drop into PDB’s command prompt. You can inspect variables, call functions, loop through a list of objects, etc. Want to know what was returned from handle_intersection()? Type the receiver variable or inspect it with dir(obj_name). When you’re ready to continue, entering “c” will continue execution, or other commands can be used to navigate through the stack, enable and disable breakpoints, etc.

Happy debugging!


Sign up for Turret.IO – the only email marketing platform specifically for developers


Leave a Reply

Your email address will not be published. Required fields are marked *