pdb and breakpoint()

RoadmapsPython

Overview

The `breakpoint()` function pauses your Python code while it is running and lets you inspect the variables inside it. Imagine watching a movie and pressing pause during a confusing scene. You can zoom in and look at the background details before you press play again. `breakpoint()` is the pause button for your code. When Python hits it, it opens an interactive terminal (called `pdb`) where you can see the exact values of your variables. ```python def calculate_discount(price): discount = price * 0.2 # The code pauses here. # You can type 'price' or 'discount' in the terminal to see their values. breakpoint() return price - discount ``` The rule is: **`breakpoint()` pauses execution and drops you into a debugger.** From there, you can type `n` (next) to run the next line, or `c` (continue) to run until the end.

It lets you find bugs instantly. Instead of adding ten `print()` statements and guessing what went wrong, you pause the code and look directly at the memory.

Where used: Local debugging, Testing scripts, Data processing

Why learn this

Code walkthrough

def run_debugger_simulator():
    print('Line 1 runs')
    print('Code pauses here (breakpoint)')
    # User types 'c' to continue
    print('Line 3 runs')

run_debugger_simulator()

Focus: print('Code pauses here (breakpoint)')

Aha moment

def test():
    c = 10
    # If you type 'c' in the debugger, what does the debugger do?
    print('Finished')

test()

Prediction: If you pause the code here and type 'c' in the debugger, does it print 10 or resume the code?

Common guess: It prints 10.

The debugger commands (like 'c' for continue) take priority over your variable names. It will resume the code.

Common mistakes

Recall questions

Understanding checks

A teammate pushes code with `breakpoint()` to the live web server. What happens when a user loads that page?

The server pauses execution on that line and waits forever for someone to type a debugger command. The user's page will time out and crash.

`breakpoint()` pauses execution and drops you into a debugger. If there is no human at the keyboard to type 'c', the code hangs forever.

You hit a breakpoint and want to check the value of a variable named 'c'. You type 'c' in the debugger. Why doesn't it show the variable's value?

Because 'c' is the built-in debugger command for 'continue'. The debugger resumes the code instead of showing the variable.

The debugger terminal has special reserved commands like 'n' (next) and 'c' (continue). If your variable shares a name with a command, the command wins.

Practice tasks

Simulate removing a breakpoint

Given this code, remove the simulated pause so that it runs straight through.

Challenge

Fix the variable name

You are writing code that you plan to debug. Rename the variable `c` to something that will not conflict with the debugger's 'continue' command.

Continue learning

Previous: Defining functions and return values

Previous: LEGB rule: variable scope in Python

Return to Python Roadmap