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

5

134

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?

Palma answered 14/1, 2014 at 20:35 Comment(1)
Kind of overkill but have you try with eval? eval('n = 3')? Maybe it helps :)Turd
H
184

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

Heigl answered 9/11, 2014 at 12:28 Comment(1)
IMO it would've been more user-friendly for commands to require ! or some such prefix.Kathyrnkati
V
13

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
Vaporous answered 15/1, 2014 at 1:46 Comment(1)
A functional workaround, but I believe '!' is the operator intended to deal with this case.Hullabaloo
T
4

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.

Typology answered 25/9, 2022 at 8:14 Comment(0)
B
2

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.

Blessington answered 14/1, 2014 at 20:41 Comment(1)
Looks interesting; I'll have to take a stab at it when I have time to learnPalma
B
0

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.

Berglund answered 14/1, 2014 at 21:3 Comment(3)
visual debuggers are usually very slow compared to debugging through console, which takes almost no time to start the debugger.Flavory
@Curious They are slower to start but are often more usable especially for those of limited experience.Berglund
For scientific work I do find Spyder's debugger handy. I don't think debugging a 10x10 matrix is practical on the CLIDecelerate

© 2022 - 2024 — McMap. All rights reserved.