w3resource

How can you inspect variables' values during debugging using pdb in Python?

Mastering Variable Inspection with 'pdb' Commands in Python Debugging

During debugging using 'pdb' (Python Debugger), you can inspect and display variable values at specific points in the code. 'pdb' provides several commands that allow you to interactively view variable values during debugging.

Here are some 'pdb' commands for inspecting variable values:

p (print):

  • Print the value of a variable or an expression.
  • Usage: p variable_name or p expression

Example:

(Pdb) p x
100
(Pdb) p numslist
[1, 2, 3, 4, 5, 6]

pp (pretty print):

  • Pretty-print the value of a variable or an expression.
  • Usage: pp variable_name or pp expression

Example:

(Pdb) pp my_dict
{'name': Andy Agi, 'age': 35, 'email': [email protected]'}

a (args):

  • Print the arguments of the current function.
  • Usage: a or args

Example:

Code:

import pdb
import pdb
def my_function(a, b, c):
     print(a + b + c)
     pdb.set_trace()
my_function(1,2,3)

Run in Spyder (Python 3.0)

In [4]: runfile('C:/Users/ME/test.py', wdir='C:/Users/ME')
6
--Return--
None
> c:\users\me\test.py(4)my_function()
      2 def my_function(a, b, c):
      3      print(a + b + c)
----> 4      pdb.set_trace()
     5 my_function(1,2,3)
IPdb [1]: args
a = 1
b = 2
c = 3

w (where):

  • Print a stack trace with the current line and the chain of function calls.
  • Usage: w or where

Code:

import pdb
def my_function():
     x = 10
     y = 20
     pdb.set_trace()
my_function()

Run in Spyder (Python 3.0)

In [1]: runfile('C:/Users/ME/test.py', wdir='C:/Users/ME')
--Return--
None
> c:\users\me\test.py(5)my_function()
      2 def my_function():
      3      x = 10
      4      y = 20
----> 5      pdb.set_trace()
      6 my_function()
IPdb [1] where
    [... skipping 21 hidden frame(s)]
  c:\users\me\appdata\local\temp\ipykernel_6208\2553308459.py(1)()
  h:\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py(528)runfile()
    526     current_namespace: if true, run the file in the current namespace
    527     """
--> 528     return _exec_file(
    529         filename, args, wdir, namespace,
    530         post_mortem, current_namespace, stack_depth=1)

    [... skipping 2 hidden frame(s)]

  h:\anaconda3\lib\site-packages\spyder_kernels\py3compat.py(356)compat_exec()
    354 def compat_exec(code, globals, locals):
    355     # Wrap exec in a function
--> 356     exec(code, globals, locals)
    357 
    358 
  c:\users\me\test.py(6)<module>()
      2 def my_function():
      3      x = 10
      4      y = 20
      5      pdb.set_trace()
----> 6 my_function()
None
> c:\users\me\test.py(5)my_function()
      2 def my_function():
      3      x = 10
      4      y = 20
----> 5      pdb.set_trace()
      6 my_function() 

l (list):>

  • Show the source code around the current line.
  • Usage: l or list

Example:

Code:

import pdb
def my_function():
     x = 10
     y = 20
     pdb.set_trace()
my_function()

Run in Spyder (Python 3.0)

In [1]: runfile('C:/Users/ME/test.py', wdir='C:/Users/ME')
--Return--
None
> c:\users\me\test.py(5)my_function()
      2 def my_function():
      3      x = 10
      4      y = 20
----> 5      pdb.set_trace()
      6 my_function()
IPdb [1] l
      1 import pdb
      2 def my_function():
      3      x = 10
      4      y = 20
----> 5      pdb.set_trace()
      6 my_function()

The above commands allow us to inspect and display variable values interactively during the debugging process.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-interview/how-can-you-inspect-variables-values-during-debugging-using-pdb-in-python.php