问题:如何处理名称与PDB命令冲突的变量?

不管是好是坏,我的代码充斥着单个字母变量(这是物理上的东西,所以这些字母是有意义的)以及我经常与之交互的NumPy。

使用Python调试器时,偶尔我会想看看的值n。但是,当我点击时n<enter>,这是PDB命令的(n)ext优先级更高。 print n可以解决问题,但是如何设置呢?

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?


回答 0

!在语句运行前使用感叹号:

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)

文件说:

! statement

在当前堆栈框架的上下文中执行(单行)语句。除非语句的第一个单词类似于调试器命令,否则可以省略感叹号。[…]

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


回答 1

您可以使用分号,因此只需在其前面加上其他内容即可:

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

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

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

回答 2

这不是您问题的直接答案,但可能会对您有所帮助:PuDB是PDB的基于控制台的可视界面,通过设计将命令与变量操作分开。

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.


回答 3

Eric IDE,Wing IDE和Spyder仅举几例都具有可视调试器,因为它们将值的显示与命令分开,因此值得一试。

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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。