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

Question:

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?

Asked By: Thierry Lam

||

Answers:

Just print it:

(Pdb) print list(values)

don’t foget to add brackets for python3 version

(Pdb) print(list(values))
Answered By: Fred Foo

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

Answered By: dc5553

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]
Answered By: elmotec

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.

Answered By: vaudt

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.

Answered By: Owen Brown
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.