使用 pdb 调试 python 时如何更改值?

发布于 2024-12-12 07:09:30 字数 1121 浏览 0 评论 0原文

我想运行 pdb,单步执行代码,并在某个时刻更改某个名称指向的值。所以我可能想更改名称“stationLat”指向的值。但我好像不能。示例如下:

>>> import extractPercentiles
>>> import pdb
>>> pdb.run( "extractPercentiles.extractOneStation()" )
> <string>(1)<module>()->None
(Pdb) s
--Call--
> /scratch/extractPercentiles.py(96)extractOneStation()
-> def extractOneStation() :
(Pdb) tbreak 132
Breakpoint 3 at /scratch/extractPercentiles.py:132
(Pdb) c

Deleted breakpoint 3
> /scratch/extractPercentiles.py(132)extractOneStation()
-> stationLon = float(stationLoc[3])

现在我想要更改 stationlat 的值。 Pdb 似乎允许我将 stationLat 设置为新值,但是当我检查该值时,它没有更改:

(Pdb) stationLat
-34.171100000000003
(Pdb) stationLat = -40
(Pdb) stationLat   
-34.171100000000003
(Pdb) !stationLat = -40
(Pdb) stationLat
-34.171100000000003
(Pdb) 

您可以看到我尝试使用!也没有成功。

pdb 手册说我应该能够更改变量:

调试器无法识别的命令被假定为 Python 语句,并在正在调试的程序的上下文中执行。 Python 语句也可以以感叹号 (!) 为前缀。这是检查正在调试的程序的有效方法; 甚至可以更改变量或调用函数

这是范围问题吗?这与我启动 pdb 的方式有关吗?我尝试了嵌入的“pdb.set_trace”习惯用法,得到了相同的结果。

感谢您的阅读。

I want to run pdb, step through the code, and at some point change the value pointed at by some name. So I might want to change the value pointed at by the name 'stationLat'. But it seems I can't. Here's the example:

>>> import extractPercentiles
>>> import pdb
>>> pdb.run( "extractPercentiles.extractOneStation()" )
> <string>(1)<module>()->None
(Pdb) s
--Call--
> /scratch/extractPercentiles.py(96)extractOneStation()
-> def extractOneStation() :
(Pdb) tbreak 132
Breakpoint 3 at /scratch/extractPercentiles.py:132
(Pdb) c

Deleted breakpoint 3
> /scratch/extractPercentiles.py(132)extractOneStation()
-> stationLon = float(stationLoc[3])

So now I'm at a place where I would like to change the value of stationlat. Pdb appears to allow me to set stationLat to a new value, but when I inspect the value, it is unchanged:

(Pdb) stationLat
-34.171100000000003
(Pdb) stationLat = -40
(Pdb) stationLat   
-34.171100000000003
(Pdb) !stationLat = -40
(Pdb) stationLat
-34.171100000000003
(Pdb) 

You can see I tried using ! as well, without success.

The pdb manual says I should be able to change variables:

Commands that the debugger doesn’t recognize are assumed to be Python statements and are executed in the context of the program being debugged. Python statements can also be prefixed with an exclamation point (!). This is a powerful way to inspect the program being debugged; it is even possible to change a variable or call a function

Is this a question of scope? Is it to do with the way I have started pdb? I tried the embedded "pdb.set_trace" idiom and I got the same result.

Thanks for reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咋地 2024-12-19 07:09:31

这似乎是 Python 2.6 中的错误。您应该能够在 Python 2.7 中执行此操作。

This appears to be a bug in Python 2.6. You should be able to do this in Python 2.7.

猫七 2024-12-19 07:09:31

实际上,当您在 pdb 中分配新值时,变量的值确实会发生变化。但是,如果您尝试在不运行代码的情况下再次读取 pdb 中的变量,它可能会重置为原始值。

如果您返回代码,您应该会看到它将使用您的新值(-40)。

试试这个:

stationLat = -40
s # step back into code
stationLat # should display -40

Actually, the value of the variable does get changed when you assign a new value in pdb. But if you try to read the variable in pdb again without running your code, it may reset to the original value.

If you step back into your code, you should see that it will use your new value (-40).

Try this:

stationLat = -40
s # step back into code
stationLat # should display -40
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文