指定end关键字后,为什么python3的print语句不刷新输出? [英] Why doesn't python3's print statement flush output when end keyword is specified?

查看:193
本文介绍了指定end关键字后,为什么python3的print语句不刷新输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from sys import argv, stdout as cout
from time import sleep as sl
print("Rewinding.......",end = '') # If end is given output isn't flushed. But why?
cout.flush()
for i in range(0,20):
    sl(0.2)
    print(".",end='',flush = True) #No output is printed if flush wasn't specified as true.

print("Done") #Output is by default flushed here

当我指定结束并进入睡眠状态时,我注意到直到下一次默认情况下将其刷新的输出才刷新.

When I specified end and sleep, I noticed that output wasn't flushed until next print where it was by default flushed.

为什么会这样?我不得不手动刷新输出.

Why does this happen? I had to manually flush the output.

推荐答案

实际上,这是基础stdio函数的默认行为.

In fact this is the default behavior of the underlying stdio functions.

当将输出输出到控制台时,遇到换行符时将自动刷新流,但不会遇到其他字符.

When the output is to console, the stream will be automatically flushed when a newline is encountered, but not other characters.

如果输出不是控制台,则即使换行也不会触发刷新.

If the output is not a console, then even newline won't trigger a flush.

如果要确保刷新,可以告诉 print() 明确地:

If you want to make sure about flush, you can tell the print() explicitly:

print("Rewinding.......",end = '',flush=True)

这篇关于指定end关键字后,为什么python3的print语句不刷新输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆