08 May 2022

pdb - The Python Debugger / breakpoint

breakpoint versus pdb

Normally we import pdb and then to use the PDB in the program we have to use one of its method named set_trace(). This is how the code goes:
import pdb
pdb.set_trace()

An easier alternative (starting from Python version 3.7 onwards) is to use the built-in breakpoint() function. This helps us to do away with explicit import

breakpoint() # in python 3.7 and above

When we call the breakpoint() function, the default implementation of the breakpoint() function will call sys.breakpointhook(), which in turn calls the pdb.set_trace() function. This is why we do not need to import pdb and call pdb.set_trace() explicitly ourselves.