How do I manipulate a variable whose name conflicts with PDB commands?


My code is, for better or worse, rife with single letter variables (it’s physics stuff, so those letters are meaningful), as well as NumPy’s, which I’m often interacting with.

When using the Python debugger, occasionally I’ll want to look at the value of, say, n. However, when I hit n<enter>, that’s the PDB command for (n)ext, which has a higher priority. print n works around looking at it, but how can I set it?

Asked By: Nick T



That is not the direct answer to your question, but it may help you: PuDB is a console-based visual interface for PDB which separates commands from variable manipulation by design.

Eric IDE, Wing IDE & Spyder to mention just a few all have visual debuggers that are worth a go as they separate the display of values from the commands.

Answered By: Steve Barnes

You can use semicolons, so just put something else in front of it:

ipdb> print n
ipdb> n
> 145 <some code here>

ipdb> 1; n=4
ipdb> print n
Answered By: Corley Brigman

Use an exclamation mark ! before a statement to have it run :

python -m pdb
> /home/user/<module>()
-> print('foo')
(Pdb) !n = 77
(Pdb) !n
(Pdb) n
> /home/user/<module>()
-> print('bar')

The docs say:

! statement

Execute the (one-line) statement in the context of the current stack frame. The exclamation point can be omitted unless the first word of the statement resembles a debugger command. […]

Answered By: Abraham

Use brackets (variable_name).

For example, if you have one variable named q, and you want to check out its value.
If you directly input q in the prompt, then the ipdb debugging process will quit and break up.

>>> q

Instead, you should input (q) to check this variable:

>>> (q)

Then its value will be printed.

Answered By: KingLiu
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.