In this article, we will learn about python breakpoint() method with examples. We will see how to use this method to debug our code whenever and where-ever we need.
Getting Started
breakpoint() method allows us to apply code debugging if we need it throughout program. In other words, it allows developer to set points in code at which debugger is called.
breakpoint() method calls sys.breakpointhook() which by default calls pdb.set_trace(). We can also achieve same functionality by importing pdb and calling pdb.set_trace().
But, breakpoint() makes this process easier. We just need to call this method, no other painful importing etc.
sys.breakpointhook() method uses environment variable PYTHONBREAKPOINT to configure debugger. If unset, default PDB debugger is called.
If PYTHONBREAKPOINT is set to “0”, no code debugging is performed. If becomes useful when we don’t want to debug our code.
Syntax of breakpoint() Method
Syntax of breakpoint() Method in python –
breakpoint(*args, **kws)
Parameters of breakpoint() Method
As shown in syntax, breakpoint() method accepts two arguments.
Examples of breakpoint() Method
Let’s see some examples of breakpoint() method to understand how debugging can be done using this method.
x = 5 y = "Hello Tutorialwing" print(x) breakpoint() print(y)
When we run above code, we get output similar as below –
5 > /home/repl/6f5c718e-43e6-4349-912e-ef1406c1bebe/main.py(7)<module>() -> print(y) (Pdb)
As we can see program opens up pdb debugger console as soon as breakpoint() is executed. Till now, only value of x is printed.
If you want to continue program execution, type c and press enter button.
After enter button, we get output as below –
> /home/repl/011d7044-c688-4df7-baf8-52472c41e376/main.py(7)<module>() -> print(y) (Pdb) c Hello Tutorialwing
As you can see, value of y is also printed now.
As you would have noticed, we needed to type c and then press enter button in order to continue program execution. In similar way, there are some other command for debugging –
Letter Description c Continue program execution. q Quit program execution. n Goto next line within same function. s Goto next line within this function or called function.
Learn more at official doc.
That’s end of our post on python breakpoint() method with examples.