Python breakpoint()

The breakpoint() function is a debugging function that is built into Python 3.7 .

Use this built in function by adding breakpoint() where you suspect the error or are wanting to check for bugs.

The pdb (Python Debugger) is a interactive source code debugger for Python programs.

Python Debugger Commands :

c -> continue execution
q -> quit the debugger/execution
n -> step to next line within the same function
s -> step to next line in this function or a called function

Ex.

def Test(x): 
    breakpoint() 
    ex = [x[obj] for obj in range(0, len(x)-2)] 
    return ex 
  
print(Test([1, 2, 3]))

Output

> /home/runner/ue53p3stxbj/main.py(3)Test()
-> ex = [x[obj] for obj in range(0, len(x)-1)]

(Pdb) c # here pdb command C is inputted

[1, 2]

Once code as been executed enter one of the commands in the terminal

Since there is no issue with the above code NO error message is prompted

The code below produce an error. The c pdb command will be used

def Test(x): 
    breakpoint() 
    ex = [x[obj] for obj in range(0, len(x)+1)] 
    return ex 
  
print(Test([1, 2, 3]))

Output

> /home/runner/ue53p3stxbj/main.py(3)Test()
-> ex = [x[obj] for obj in range(0, len(x)+1)]

(Pdb) c

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    print(Test([1, 2, 3])) 
  File "main.py", line 3, in Test
    ex = [x[obj] for obj in range(0, len(x)+1)] 
  File "main.py", line 3, in <listcomp>
    ex = [x[obj] for obj in range(0, len(x)+1)] 
IndexError: list index out of range
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s