python 如何边改代码边调试_在调试器中更改Python代码

Is there any debugger that allows Python code to be changed while debugging?

In other words: run-time exception occurs, debugger stops, I change the code any way I like, and tell the program to continue.

I am aware of the problems with this approach, such that references to functions would still point to the old definitions if I redefine the function on the fly, and so on. I am ok with that, since I just want to be able to make small fixes in very simple circumstances.

On the other hand, I'm also interested in whether it's theoretically possible to allow changes to Python code without running into these problems: i.e., somehow update all the references to the objects that changed, etc. I'm nearly sure the answer to the second question is no, but if I'm wrong, I'd like to know.

EDIT: If my goal (changing the code interactively when an exception occurred, and then continuing execution), is achievable without a debugger - that would be good as well. I don't need to use the debugger.

解决方案

Since you can change the contents of regular classes the way you want at any time, there's no need to update references to objects: you just update class's __dict__ with new methods and other attributes.

The problem is with references to functions: you can't update a function without changing its identity. You can use proxy functions that always look up real functions by name, and you change real functions at any moment. Else, you plan your code so that it doesn't store function references for long; once a function is updated, it will soon be looked up by name, but old references passed somewhere will continue to execute for a bit longer.

Such patching would make sense on a long-running system when you want to upgrade it without serious downtime: you pause it for a moment to upgrade several classes and functions consistently and un-pause. AFAIK Erlang does its on-the-fly updates in a similar way.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值