简体   繁体   English

Python:默认情况下是否可以使“打印”刷新?

[英]Python: Is it possible to make 'print' flush by default?

The print function in Python 3 may receive a boolean value to whether flush immediately or not: Python 3 中的print function 可能会收到一个 boolean 值来确定是否立即刷新:

print("foobar", flush=True)

I wish to have the print function flush by default for everything it prints, is it possible?我希望print function 默认刷新它打印的所有内容,这可能吗? Work-around, ad-hoc settings, whatever.解决方法,临时设置,等等。

You can run Python in unbuffered mode with:您可以在无缓冲模式下运行 Python:

python -u

Or set the environment variable或者设置环境变量

PYTHONUNBUFFERED=TRUE

For me this does looks like task for functools.partial , but I am not sure if it would work in this case, so can you please try following: add at begin of your file对我来说,这看起来像是functools.partial的任务,但我不确定它在这种情况下是否有效,所以请您尝试以下操作:在文件开头添加

import functools
print = functools.partial(print, flush=True)

and test if it does what you want?并测试它是否符合您的要求?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM