2014-09-03 20 views
2

假設我正在使用Eclipse的PyDev代碼。是否有可能在調試時修改python代碼?與Pydev,PDB等

有沒有辦法在斷點下面添加一些新代碼...在運行時/運行時修改代碼?

+0

好了,你_could_想出了一些代碼,允許你,例如,'imp.reload '一個模塊。如果你編寫的代碼主要是無狀態響應(比如web框架),並且只希望下一個請求獲得新代碼,那很容易(事實上,像Flask這樣的許多框架會自動完成)。如果你正在處理有狀態的代碼,你必須保留你的狀態對象的弱點,以便醃製 - 取消所有的東西。如果你想影響當前的功能堆棧,你需要一個函數來重建幀。這一切都是可行的(至少在CPython/PyPy中)......但並不容易,也可能是一個糟糕的主意。 – abarnert 2014-09-03 00:35:28

+0

重新導入模塊可能是可能的,但這是一個比OP要求的機制更有限的機制。打4秒! – synthesizerpatel 2014-09-03 00:35:33

回答

0

在PDB中,可以使用p來評估和打印表達式和!執行atements:

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) 

文檔:https://docs.python.org/2/library/pdb.html

至於如何做到這一點的PyDev,我不使用PyDev的,所以我不太清楚;但是這個功能應該可用於任何基於BDB的調試器(我相信PyDev也是基於BDB的?)。

相關問題