Why do python print statements that contain 'end=' arguments behave differently in while-loops?

后端 未结 2 394
故里飘歌
故里飘歌 2020-12-20 19:34

I\'m running python version 2.7.3 on MacOSX.

Consider this block of code:

from __future__ import print_function
import time
x = 0
while x < 5:
            


        
2条回答
  •  春和景丽
    2020-12-20 20:08

    This is just python buffering stdout. This answer has some more info.

    You can flush it like this:

    import sys
    from __future__ import print_function
    import time
    x = 0
    while x < 5:
        print(x, end='')
        x += 1
        sys.stdout.flush()
        time.sleep(1)
    

    Alternatively start python python -u and it won't be buffered.

提交回复
热议问题