使用 Windows 7、python.exe 3.8.0 和 PyCharm 2019.4.5 以及:
这个(有问题的)程序
print("hello", flush=True)
garbage;
看似随机给出:
或
与“-u”类似:
为什么我每次都得到不一样的结果?
注意:我并不是在寻求补救措施。只是问一下原因而已。
这不是 PyCharm 特有的,甚至不是 Python 特有的。对不同文件执行写入时没有定义顺序。打印写入 stdout,回溯写入 stderr。
如果写入 stdout、stderr 到同一个地方,顺序将是正确的:
import sys
sys.stderr = sys.stdout
print("hello out", flush=True)
garbage;