from __future__ import print_function 没有按预期工作

from __future__ import print_function not working as intended

我已经使用答案 here 打印在 Python 2 的同一行中。

from __future__ import print_function
import time

num = '\r'
for i in xrange(10):
    num += str(i+1) + ' '
    print (num, end='')
    time.sleep(0.3)
print('\n')

但它只在循环结束后打印整个字符串。这里有什么问题?

Python 2.7.9.

链接答案中的评论给出了原因 - sys.stdout.flush() 仍然需要刷新输出缓冲区。

from __future__ import print_function
import time

num = '\r'
for i in xrange(10):
    num += str(i+1) + ' '
    print (num, end='')
    sys.stdout.flush()
    time.sleep(0.3)
print('\n')