Python breakpoint() - Examples


Python - breakpoint()

The breakpoint() function is a built-in Python debugging utility introduced in Python 3.7. It simplifies the process of debugging by calling sys.breakpointhook(), which launches the Python Debugger (pdb) by default. Using breakpoint(), you can pause your program's execution and inspect or manipulate the current state of variables interactively.

This tutorial demonstrates how to use breakpoint() effectively for debugging Python applications.


Syntax of breakpoint()

breakpoint(*args, **kwargs)

### Parameters:

  • *args, **kwargs: These are passed directly to the sys.breakpointhook(). By default, they are not required unless customizing the debugger behavior.

Examples

1. Setting a Breakpoint in a Conditional Statement

This example demonstrates how to use breakpoint() within an if statement to pause execution and inspect variables.

Python Program

a = 2
b = 3

if a + b == 5:
    breakpoint()  # Pause execution here
    print('The sum is 5')

Explanation:

  • The program pauses at the breakpoint() line if the condition a + b == 5 evaluates to True.
  • In the debugger, you can inspect the values of a, b, and other program state variables.

Debugger Output:

C:\workspace\python>python example.py
> c:\workspace\python\example.py(6)<module>()
-> print('The sum is 5')
(Pdb) a
2
(Pdb) b
3
(Pdb) a + b
5

2. Debugging a Loop

Use breakpoint() to debug a loop and monitor its execution step-by-step.

Python Program

numbers = [10, 20, 30, 40]
sum_result = 0

for num in numbers:
    sum_result += num
    if sum_result > 50:
        breakpoint()  # Pause execution when the condition is met

print('Final Sum:', sum_result)

Explanation:

  • The program calculates the cumulative sum of the numbers in the list.
  • When sum_result > 50, the execution pauses, and you can inspect num and sum_result.

Debugger Output:

(Pdb) num
30
(Pdb) sum_result
60

3. Customizing the Debugger

You can customize the behavior of breakpoint() by overriding the PYTHONBREAKPOINT environment variable.

Example: Disabling the Debugger

To disable the debugger, set PYTHONBREAKPOINT=0 before running your program:

export PYTHONBREAKPOINT=0
python example.py

Example: Using a Custom Debugger

You can use third-party debuggers like pdb++ by setting PYTHONBREAKPOINT=pdb.set_trace or any other debugger hook.


4. Debugging a Function

This example demonstrates how to debug the execution flow inside a function.

Python Program

def calculate_area(length, width):
    breakpoint()  # Pause inside the function
    return length * width

area = calculate_area(5, 10)
print('Area:', area)

Explanation:

  • Execution pauses at the breakpoint() line, allowing you to inspect the function's local variables.

Debugger Output:

(Pdb) length
5
(Pdb) width
10
(Pdb) length * width
50

Use Cases

breakpoint() is useful for:

  • Diagnosing issues in a specific block of code.
  • Debugging loops, functions, or conditionals.
  • Pausing execution to inspect program state dynamically.

Summary

In this tutorial, we explored how to use the breakpoint() function to debug Python applications. By strategically placing breakpoint(), you can pause execution, inspect variables, and step through your code interactively. Customize the debugger with environment variables for advanced use cases.


Python Libraries