Do any Python environments support edit-and-continue?
Is it possible in Python to make changes to code that you're currently debugging and continue without having to restart everything? (The way I can in C#, for ex开发者_StackOverflow社区ample.)
You have the full power of the interpreter at your fingertips with pdb
, use
import pdb
pdb.set_trace()
From there you can define and create new objects, redefine existing ones, modify and reload modules, etc. Syntax is simalar to gdb
. If you're using ipython, ipdb
may be a nicer choice, or you can automatically invoke the debugger with the ipython magic function %debug
.
You can give new values to names, but there might still be places that refer to the old value. Check the session below at the python shell:
>>> def foo(): print 'Foo'
...
>>> bar = foo
>>>
>>> foo()
Foo
>>> bar()
Foo
>>>
>>> def foo(): print 'Bar'
...
>>> foo()
Bar
>>> bar()
Foo
>>>
>>> def call(): foo()
...
>>> call()
Bar
>>>
>>>
>>> def foo(): print 'Foo reloaded!'
...
>>>
>>> foo()
Foo reloaded!
>>> bar()
Foo
>>>
>>> call()
Foo reloaded!
>>>
The function call
refers to the new foo
, but bar
was assigned the value of the old foo
and that doesn't change when you reuse the name foo
.
Were you asking for editor support? Python-mode in Emacs can run an "inferior python process" to which you can send blocks of code as you play with them. Or you could do this directly in the Python shell, as shown above.
精彩评论