Skip to content

Instantly share code, notes, and snippets.

@marvintensuan
Last active November 20, 2021 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marvintensuan/11ebe670187943f277017e5a7a0e0bb4 to your computer and use it in GitHub Desktop.
Save marvintensuan/11ebe670187943f277017e5a7a0e0bb4 to your computer and use it in GitHub Desktop.
Script to show print buffer in Python's print function.
'''This scripts aims to demonstrate the behavior of the `flush` parameter
in Python's `print` function. The for-loop below should produce
the following output:
0123456789
You may modify `END` to change this.
Set `IS_BUFFERED` to True to change the manner in which `i` is printed.
Lastly, you may experiment with running this script with -u flag.
`python -u buffer.py`
You may also experiment with manually setting the environment variable
PYTHONUNBUFFERED either via command line or via os.environ.'''
from time import sleep
IS_BUFFERED = False
END = ""
# import os
# os.environ['PYTHONUNBUFFERED'] = ""
# print(f"{os.getenv('PYTHONUNBUFFERED') = }")
for i in range(10):
print(i, end=END, flush=IS_BUFFERED)
sleep(0.5)
from rich import print
print("[white on red] Hello [/] [green]World!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment