我正在调试一些有一些永无止境的循环问题的代码,有时我无法弄清楚如何制作信息丰富的打印语句来帮助确定哪个循环被破坏了。
一个例子是:
def fake_function():
print 'I am about to enter a broken loop.'
while True:
# Doesn't matter what I'm doing here.
_ = None
然后,当我调用 时fake_function
,在停止之前我不会得到任何输出。
一个稍微大一点的例子可以说明为什么我想要这个看起来像:
def another_fake_function():
# This loop is fine and not broken.
print 'I am entering a loop that might be broken.'
leave_loop = False
while not leave_loop:
# Do a thing.
leave_loop = True
# This loop actually is broken.
print 'I am about to enter a different loop that might be broken.'
while True:
# Doesn't matter what I'm doing here.
_ = None
那么,有什么方法可以真正强制 python 立即打印一些东西?
谢谢!