Notes

`breakpoint()` Function in Python [ English ]

< Prev Next >

1. Introduction

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.

2. Syntax

breakpoint(*args, **kwargs)

Parameters

Return Value

3. Basic Example

x = 10
y = 5

breakpoint()

z = x + y
print(z)

Explanation

  1. The program starts executing normally.
  2. When Python reaches breakpoint(), execution pauses.
  3. The debugger starts, allowing you to inspect variables such as x and y.
  4. After debugging, the program continues and prints the result.

4. Example: Checking Variable Values

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.

5. Example: Finding Errors

num1 = 15
num2 = 0

breakpoint()

result = num1 / num2
print(result)

Explanation

6. Using breakpoint() Inside a Loop

for i in range(5):
    breakpoint()
    print(i)

Explanation

7. Common Debugger Commands

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

8. Internally How breakpoint() Works

By 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.

9. Advantages of breakpoint()

10. Summary

< Prev Next >