调试时是否可以修改python代码?与Pydev、PDB等合作

调试时是否可以修改python代码?与Pydev、PDB等合作,python,eclipse,debugging,pydev,Python,Eclipse,Debugging,Pydev,假设我正在使用Eclipse的PyDev逐步完成代码 有没有办法在断点下添加一些新代码。。。动态/在运行时修改代码?在PDB中,您可以使用p计算和打印表达式和!要执行声明,请执行以下操作: p expression Evaluate the expression in the current context and print its value. [!]statement Execute the (one-line) statement in the context of the cur

假设我正在使用Eclipse的PyDev逐步完成代码


有没有办法在断点下添加一些新代码。。。动态/在运行时修改代码?

在PDB中,您可以使用p计算和打印表达式和!要执行声明,请执行以下操作:

p expression

Evaluate the expression in the current context and print its value.

[!]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. To set a global variable, you can prefix the assignment command with a global command on the same line, e.g.:

(Pdb) global list_options; list_options = ['-l']
(Pdb)
文件:


至于如何在PyDev中实现这一点,我不使用PyDev,所以我不太确定;但这一功能应该适用于任何基于BDB的调试器(我相信PyDev也是基于BDB的?)。

我在github上发现了一个有趣的项目:

将代码注入到运行的Python进程中


嗯,你可以想出一些代码,允许你,例如,
imp.reload
a模块。如果您编写的代码大多是无状态响应(如web框架),并且只希望下一个请求获得新代码,那么这很容易(事实上,许多框架,如Flask,都会自动完成这项工作)。如果您处理的是有状态代码,那么您必须保留一个弱的状态对象来pickle unpickle所有内容。如果要影响当前函数堆栈,则需要一个函数来重建帧。这一切都是可行的(至少在CPython/PyPy中是如此)…但一点也不容易,而且可能是个坏主意。可以重新导入一个模块,但这将是一种比OP要求的更为有限的机制。被4秒击败!