Python breakpoint() Function

The breakpoint() function in Python drops into the debugger at the call site. That is, after calling the function breakpoint() into your Python program, the interpreter passes the control to the debugger. For example:

mylist = list()
for i in range(10):
    a.append(i)
    if i == 5:
        breakpoint()
print(mylist)

The snapshot given below shows the initial output produced by this Python program:

python breakpoint function

That is, when the value of i becomes equal to 5, then the condition of if evaluates to be True, therefore program flow goes inside the if block and the statement:

breakpoint()

gets executed. So the interpreter passed the control to the debugger. That means, no further execution of the code processed. Now using the debugger, we can execute and debug the code. For example, let me type print(mylist) to print the previously defined list, as shown in the snapshot given below:

python breakpoint function example

That is, before the execution of breakpoint(), the element 0, 1, 2, 3, 4, 5 were appended in the list named mylist. Therefore the print(mylist) prints the list. Now let's type help and press ENTER key to get all the command to work while using this Python debugger, like shown in the snapshot given below:

python breakpoint function program

Important - You can use the continue keyword to continue the execution of the code, to get/print the final output, and close the debug, as shown in the snapshot given below:

python breakpoint method

Python breakpoint() Function Syntax

The syntax of breakpoint() function is:

breakpoint(*args, **kws)

The breakpoint() function calls sys.breakpointhook(), passing the args, and kws straight through.

By Default - The sys.breakpointhook() calls pdb.set_trace() expecting no arguments.

Note - The function breakpoint() provides convenient way to use the same without explicitly importing the pdb, to enter the debugger. That is, use breakpoint() directly, without importing anything, to enter into the debugger.

Python breakpoint() Function Example

Here is an example of breakpoint() function in Python:

x = [1, 2, 4, 5]
print(x)

breakpoint()

x = [10, 20, 30]
print(x)

x = ["codes", "cracker"]
print(x)

The output produced by this Python program, demonstrating the breakpoint() function, is shown in the snapshot given below:

python breakpoint method example

After the use of breakpoint(), the last two statements were not interpreted by the interpreter. Because after the use of breakpoint(), interpreter passed the control to the debugger. But don't worry, we can execute the code, in the debug mode too. For example, the next command is used to execute the next statement after the breakpoint() function. Before using the next command, let me use the print(x) to see what will be the output:

python breakpoint method program

See, the list x still refers to the list with elements 1, 2, 4, 5, means that the statement:

x = [10, 20, 30]

is not executed. Therefore let me use the next command to execute the next statement and use the print(x) to print the list x with newly assigned list items, as shown in the snapshot given below:

python breakpoint function code

You can use continue command to continue the execution of the code, after the breakpoint() to execute all the remaining codes and stops the execution of the program, like shown in the snapshot given below:

python breakpoint

A lot of things you can do with your own debugging window created using your own code.

Python Online Test


« Previous Function Next Function »



Like/Share Us on Facebook 😋