breakpoint() and pdb Basics¶
Use breakpoint() to start the debugger and understand pdb for interactive debugging.
breakpoint() Function¶
Add breakpoints to pause execution for inspection.
def calculate(x, y):
result = x + y
breakpoint() # Debugger will start here
return result * 2
# When executed, this will start pdb debugger
# You can inspect variables and step through code
print("Debugger example")
Debugger example
> /path/to/script.py(3)calculate()
-> breakpoint()
pdb Basics¶
Understand pdb debugger and basic commands.
import pdb
def debug_function(items):
total = 0
for i, item in enumerate(items):
# Start debugger at specific point
if item > 5:
pdb.set_trace()
total += item
return total
# Debugger commands:
# l - list code
# n - next line
# s - step into function
# c - continue
# p variable - print variable
# pp dict - pretty print
result = debug_function([1, 2, 3, 6, 7])
print(f"Result: {result}")
Result: 19