Python - Debugging

9.
How can you use the pdb.set_trace() function for manual debugging?

The pdb.set_trace() function in Python is used for manual debugging. When executed, it pauses the program's execution and activates the Python Debugger (pdb), allowing developers to interactively inspect variables, step through code, and diagnose issues.

Here's an example program demonstrating the use of pdb.set_trace():

def calculate_average(numbers):
    total = 0
    count = 0

    for num in numbers:
        total += num
        count += 1

    import pdb; pdb.set_trace()  # Set breakpoint

    average = total / count
    return average

# Example: Valid input
numbers_valid = [10, 20, 30, 40, 50]
result_valid = calculate_average(numbers_valid)
print("Average:", result_valid)
<... (Python Debugger)
> /path/to/debugging_example.py(11)calculate_average()
-> import pdb; pdb.set_trace()
(Pdb) c
Average: 30.0

In this example, the pdb.set_trace() line is placed after the loop in the calculate_average function. When the program reaches this point during execution, it pauses, and the interactive pdb prompt is activated. You can then use various pdb commands like list, print, and continue to inspect variables and control the program's execution.

After continuing the execution (c command), the program completes, and the final result is printed.


10.
Explain the use of the -m pdb command line option for debugging scripts.

The -m pdb command line option in Python is used to run a script with the Python Debugger (pdb) activated. It allows developers to debug their scripts directly from the command line, providing an interactive debugging environment.

Here's an example program demonstrating the use of -m pdb:

# Filename: debugging_script.py

def calculate_average(numbers):
    total = 0
    count = 0

    for num in numbers:
        total += num
        count += 1

    average = total / count
    return average

# Example: Valid input
numbers_valid = [10, 20, 30, 40, 50]
result_valid = calculate_average(numbers_valid)
print("Average:", result_valid)

To run the script with -m pdb, use the following command in the terminal:

python -m pdb debugging_script.py
 > /path/to/debugging_script.py(3)<module>()
-> def calculate_average(numbers):
(Pdb) c
Average: 30.0

In this example, the script debugging_script.py is run with the -m pdb option. It pauses at the first line of the script and activates the pdb prompt. You can then use various pdb commands like list, print, and continue to inspect variables and control the script's execution.

After continuing the execution (c command), the script completes, and the final result is printed.


11.
How do you inspect variable values during debugging in Python?

Inspecting variable values during debugging in Python can be done using the Python Debugger (pdb) module. The pdb allows developers to interactively examine and print the values of variables at different points in the code.

Here's an example program demonstrating how to inspect variable values during debugging:

def calculate_average(numbers):
    total = 0
    count = 0

    for num in numbers:
        total += num
        count += 1

    import pdb; pdb.set_trace()  # Set breakpoint

    average = total / count
    return average

# Example: Valid input
numbers_valid = [10, 20, 30, 40, 50]
result_valid = calculate_average(numbers_valid)
print("Average:", result_valid)
<... (Python Debugger)
> /path/to/debugging_example.py(11)calculate_average()
-> import pdb; pdb.set_trace()
(Pdb) p total
50
(Pdb) p count
5
(Pdb) c
Average: 30.0

In this example, the pdb.set_trace() line is used to set a breakpoint. When the program reaches this point during execution, it pauses, and the interactive pdb prompt is activated. You can use the p command followed by the variable name to print the value of a variable. After inspecting the variables, continuing the execution (c command) allows the program to complete, and the final result is printed.


12.
Discuss the use of the breakpoint() built-in function in Python 3.7 and later.

The breakpoint() function is a built-in function introduced in Python 3.7 for setting breakpoints in code during debugging. It provides a convenient way to pause the execution of a program and enter the Python Debugger (pdb) interactive prompt.

Here's an example program demonstrating the use of breakpoint():

def calculate_average(numbers):
    total = 0
    count = 0

    for num in numbers:
        total += num
        count += 1

    breakpoint()  # Set breakpoint

    average = total / count
    return average

# Example: Valid input
numbers_valid = [10, 20, 30, 40, 50]
result_valid = calculate_average(numbers_valid)
print("Average:", result_valid)
<... (Python Debugger)
> /path/to/debugging_example.py(11)calculate_average()
-> breakpoint()
(Pdb) p total
50
(Pdb) p count
5
(Pdb) c
Average: 30.0

In this example, the breakpoint() function is used to set a breakpoint. When the program reaches this point during execution, it pauses, and the interactive pdb prompt is activated. You can use the p command followed by the variable name to print the value of a variable. After inspecting the variables, continuing the execution (c command) allows the program to complete, and the final result is printed.