Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pdb

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?

like image 557
Nick T Avatar asked Jan 14 '14 20:01

Nick T


People also ask

How do you use breakpoint in pdb?

Optionally, you can also tell pdb to break only when a certain condition is true. Use the command b (break) to set a breakpoint. You can specify a line number or a function name where execution is stopped. If filename: is not specified before the line number lineno , then the current source file is used.

What is pdb debugging?

The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame.

How do I exit interactive mode in pdb?

If you enter pdb interactive mode there is no way to return to ipdb or ipython. The proper way to exit is by using ctrl-d .

How do I run a pdb script?

Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally and execution will stop where we have introduced a breakpoint.


2 Answers

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

like image 99
Abraham Avatar answered Oct 06 '22 00:10

Abraham


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 
like image 31
Corley Brigman Avatar answered Oct 05 '22 22:10

Corley Brigman