Writing code in Python is not always an easy task. Often writing “perfect” code without any errors is nothing more than a coder’s daydream. Allow me to introduce you to a little function programmers use to debug their code called the breakpoint()
function.
Now there are several tools a programmer can use in their coding environments such as debuggers and linters, but what if you don’t have access to these tools? You’re left with an option that is built right into Python ITSELF!!
Python breakpoint()
The breakpoint()
function was given to us in Python version 3.7. Before breakpoint()
coders would use a module called Python Debugger. PDB would need to be imported into the session with import pdb
at the top of the script and then called upon using the variable pb.set_trace()
.
But with the Python breakpoint()
function, you can call on it inside the script you are wanting to debug without having to import any new modules, freeing up time and space in your workflow. That is because breakpoint()
is built right into Python 3.7+.
breakpoint()
You can then run the code in that script and the breakpoint()
function will print the output, line by line, and begin debugging. If an error occurs or a bug is found, it will stop the output, allowing the programmer to pinpoint the error. Then he or she can then rerun the script to see if it passes. If the script executes without error, then you have written “perfect” code!
Let’s look at an example:
a = [] for i in range(5): a.append(i) if i == 4: breakpoint() print(a)
Here we can see that we’ve added a breakpoint()
function to our script and it opened the Python Debugger. The code runs only until it arrives at the first breakpoint. Then, it waits for your user input:
Here’s a list of commands, you can give it:
h
: Helpw
: wheren
: nexts
: step (step into the function)c
: continuep
: printl
: listq
: quit
Once you’ve finished debugging your code you can type "c"
to continue and exit Python’s Debugger module.
Here’s how you can print the value of the variables at the given breakpoint:
With the command p i
you print the value of variable i
(which is 4 at the breakpoint) and with p a
you print the value of variable a
which is [0, 1, 2, 3, 4]
.
You can read more about these PDB commands here: https://docs.python.org/3/library/pdb.html
Adding a debugger to your workflow greatly improves your coding capabilities and as result, you will become more productive and you will also have less stress. By using a debugger such as Python’s breakpoint()
function at the very beginning of your workflow, you will then start to write more efficient code because you are fixing the errors as you go instead of waiting until the very end. Examining each individual line of code allows you to pinpoint problems as they occur and not have dozens of errors when you finish writing out your entire script.
Whether you use a debugging tool with your IDE or you use built-in functions like breakpoint
, try incorporating debugging into your workflow today. 🙂