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

后端 未结 2 400
故里飘歌
故里飘歌 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:13

    Because the output stream is line-buffered - since it's not explicitly being flushed in between print statements, it waits until it sees a newline to flush the output.

    You can force flushing via sys.stdout.flush().

    Alternatively if you run Python with the -u flag, it will disable line buffering.

提交回复
热议问题