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

PythonPdb

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

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

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. [...]

Solution 2 - Python

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

ipdb> print n
2
ipdb> n
> 145 <some code here>
  146
  147

ipdb> 1; n=4
1
ipdb> print n
4

Solution 3 - Python

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.

Solution 4 - Python

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.

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
QuestionNick TView Question on Stackoverflow
Solution 1 - PythonAbrahamView Answer on Stackoverflow
Solution 2 - PythonCorley BrigmanView Answer on Stackoverflow
Solution 3 - PythonBartosz MarcinkowskiView Answer on Stackoverflow
Solution 4 - PythonSteve BarnesView Answer on Stackoverflow