0

I was debugging some code that had some never-ending loop issues, and I sometimes couldn't figure out how to make informative print statements to help identify which loop was broken.

An example case is:

def fake_function():
    print 'I am about to enter a broken loop.'

    while True:
    #  Doesn't matter what I'm doing here.
        _ = None

When I then call fake_function, I don't get any output until I halt it.

A slightly larger example that might show why I wanted this looks like:

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

So, is there any way to really force python to print something immediately?

Thanks!

  • If you put a print inside the loop, its pretty obvious that the loop is broken, no? – Loocid May 07 '15 at 01:05
  • Thanks Ben! Didn't know the term I was looking for was flush. – ppaattrriicckk May 07 '15 at 01:19
  • 1
    It still annoys me that using the dup-hammer deletes [Ben N](http://stackoverflow.com/users/2825369/ben-n) comment so he doesn't get the credit for finding the dup. I know that credit is worth 0 rep and isn't supposed to count for anything, and comments are ephemeral and meaningless, but still… – abarnert May 07 '15 at 01:36

0 Answers0