How do I call the Python's list while debugging?

PythonDebugging

Python Problem Overview


I have the following python code:

values = set([1, 2, 3, 4, 5])
import pdb
pdb.set_trace()

I run the script and I am in the debugging shell:

(pdb) list(values)
*** Error in argument: '(values)'
(Pdb)

How can I call list(values) in the debugger without invoking the debugger's own list command?

Python Solutions


Solution 1 - Python

Just print it:

(Pdb) print list(values)

don't foget to add brackets for python3 version

(Pdb) print(list(values))

Solution 2 - Python

Use the exclamation mark ! to escape debugger commands:

(Pdb) values = set([1, 2, 3, 4, 5])
(Pdb) list(values)
*** Error in argument: '(values)'
(Pdb) !list(values)
[1, 2, 3, 4, 5]

Solution 3 - Python

Thierry,

Since this data structure is already an sequence it is redundant to specify it as a list. So this will work fine.

(Pdb) print values

or

(Pbd) print(values)

if you are using Python3


Optionally for a nice listing with newlines

(Pdb) for x in values:  print x

or

(Pdb) for x in values:  print(x)

for Python3

Solution 4 - Python

Enter "Interactive Mode" by typing interact.

(Pdb) heros = ['Gecko', 'Catboy', 'Owlette']
(Pdb) list(heros)
*** Error in argument: '(heros)'
(Pdb) interact
*interactive*
>>> list(heros)
['Gecko', 'Catboy', 'Owlette']

"Interactive Mode" also lets you address variables whose name conflict with PDB commands.

Solution 5 - Python

Another somewhat hackerish way to do it is type:

lst=list

at the pdb prompt. Next you can write:

values = set([1, 2, 3, 4, 5])
lst(values)

Obviously this is not the recommended answer.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThierry LamView Question on Stackoverflow
Solution 1 - PythonFred FooView Answer on Stackoverflow
Solution 2 - PythonelmotecView Answer on Stackoverflow
Solution 3 - Pythondc5553View Answer on Stackoverflow
Solution 4 - PythonOwen BrownView Answer on Stackoverflow
Solution 5 - PythonvaudtView Answer on Stackoverflow