我想编写可以逐行执行其他Python代码的Python代码,但是没有与pdb一样的用户交互。我基本上需要exec()
,但是一次只需要一行。你知道吗
例如,我想编写如下代码:
import non_interactive_pdb as ni_pdb
def interleave(code1, code2):
'''"Interleave" the execution of code1 and code2, which are two strings
representing Python source code, by alternately running one line
from code1 and followed by one line from code2.'''
prog1 = ni_pdb.compile(code1)
prog2 = ni_pdb.compile(code2)
while not prog1.terminated() and not prog2.terminated():
prog1.exec_next_line()
prog2.exec_next_line()
if prog1.terminated():
while not prog2.terminated():
prog2.exec_next_line()
if prog2.terminated():
while not prog1.terminated():
prog1.exec_next_line()
这只是一个例子。我确信有一种更有效的方法可以达到与上述代码相同的效果——例如exec(code1); exec(code2)
——但这不是我想要的。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐