The breakpoint() function in Python is a built-in debugging function used to pause the execution of a program and start a debugging session. It allows programmers to inspect variables, evaluate expressions, and understand how the program is executing step by step.
The function was introduced in Python 3.7 to provide an easier and standardized way to invoke the Python debugger.
When breakpoint() is encountered during program execution, Python temporarily stops the program and opens the debugger, allowing the programmer to analyze the program state.
breakpoint(*args, **kwargs)
Parameters
*args and **kwargs are optional arguments that may be passed to the underlying debugging system.Return Value
x = 10
y = 5
breakpoint()
z = x + y
print(z)
Explanation
breakpoint(), execution pauses.x and y.a = 20
b = 4
result = a / b
breakpoint()
print("Result:", result)
Explanation
When the debugger starts, you can check variables by typing commands such as:
p a
p b
p result
Here p means print the value of a variable.
num1 = 15
num2 = 0
breakpoint()
result = num1 / num2
print(result)
Explanation
breakpoint().num2 and realize that it is 0, which would cause a division by zero error.breakpoint() Inside a Loopfor i in range(5):
breakpoint()
print(i)
Explanation
i changes during execution.When breakpoint() starts the debugger, some common commands can be used:
| Command | Meaning |
|---|---|
p variable |
Print the value of a variable |
n |
Execute the next line |
c |
Continue program execution |
q |
Quit debugging |
l |
Show the current code |
Example:
(Pdb) p x
10
breakpoint() WorksBy default, breakpoint() calls the Python debugger module (pdb) internally.
It is equivalent to writing:
import pdb
pdb.set_trace()
Therefore, breakpoint() is simply a simpler and cleaner way to start the debugger.
breakpoint()pdb.set_trace()breakpoint() is a built-in debugging function introduced in Python 3.7.pdb).