为什么这个两行Python程序在PyCharm下给出变量输出?

问题描述 投票:0回答:1

使用 Windows 7、python.exe 3.8.0 和 PyCharm 2019.4.5 以及:

enter image description here

这个(有问题的)程序

print("hello", flush=True)
garbage;

看似随机给出:

enter image description here

与“-u”类似:

enter image description here

为什么我每次都得到不一样的结果?

注意:我并不是在寻求补救措施。只是问一下原因而已。

python python-3.x pycharm
1个回答
0
投票

这不是 PyCharm 特有的,甚至不是 Python 特有的。对不同文件执行写入时没有定义顺序。打印写入 stdout,回溯写入 stderr。

如果写入 stdout、stderr 到同一个地方,顺序将是正确的:

import sys
sys.stderr = sys.stdout
print("hello out", flush=True)
garbage;
© www.soinside.com 2019 - 2024. All rights reserved.