简体   繁体   中英

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:

print("foobar", flush=True)

I wish to have the print function flush by default for everything it prints, is it possible? Work-around, ad-hoc settings, whatever.

You can run Python in unbuffered mode with:

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

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

and test if it does what you want?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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