Skip to content

Commit 9dac9fe

Browse files
Refactor stdout flushing note for clarity
Removed duplicate text and improved clarity in the note about stdout flushing behavior.
1 parent 33410ab commit 9dac9fe

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

Doc/library/functions.rst

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,33 +1618,34 @@ are always available. They are listed here in alphabetical order.
16181618

16191619
.. note::
16201620

1621-
In Python, printing a string containing newline characters does not automatically flush stdout.
1622-
Python performs buffering at the write/operation level, so newlines inside a single write
1623-
do not necessarily trigger an immediate flush. The exact timing of output may vary depending
1621+
In Python, printing a string containing newline characters does not automatically flush stdout.
1622+
Python performs buffering at the write/operation level, so newlines inside a single write
1623+
do not necessarily trigger an immediate flush. The exact timing of output may vary depending
16241624
on the environment:
16251625

1626-
- When stdout is connected to a terminal (TTY), output is line-buffered and typically flushes
1626+
- When stdout is connected to a terminal (TTY), output is line-buffered and typically flushes
16271627
after the write completes.
1628-
- When stdout is redirected to a file or pipe, output may be fully buffered and not flush
1628+
- When stdout is redirected to a file or pipe, output may be fully buffered and not flush
16291629
until the buffer fills or flush is requested.
16301630

1631-
For guaranteed immediate output, use ``flush=True`` or call ``sys.stdout.flush()`` explicitly.
1632-
Running Python with the ``-u`` flag also forces unbuffered output, which may be useful in
1631+
For guaranteed immediate output, use ``flush=True`` or call ``sys.stdout.flush()`` explicitly.
1632+
Running Python with the ``-u`` flag also forces unbuffered output, which may be useful in
16331633
scripts requiring immediate writes.
16341634

16351635
Example:
16361636

16371637
.. code-block:: python
1638-
16391638
from time import sleep
16401639
1641-
print("Hello\nWorld", end='') # Both lines appear together on TTY
1640+
# Whether the default end is a newline ('\\n') or any other character,
1641+
# Python performs a single write operation for the entire string.
1642+
# Therefore, newlines inside the string do not cause mid-string flushing.
1643+
print("Hello\nWorld")
16421644
sleep(3)
16431645
print("Hi there!")
16441646
1645-
.. versionchanged:: 3.3
1646-
Added the *flush* keyword argument.
1647-
1647+
.. versionchanged:: 3.3
1648+
Added the *flush* keyword argument.
16481649

16491650
.. class:: property(fget=None, fset=None, fdel=None, doc=None)
16501651

0 commit comments

Comments
 (0)