Skip to content

Commit 1926974

Browse files
Refactor stdout flushing note for clarity
Removed duplicate text and improved clarity in the note about stdout flushing behavior.
1 parent 7cc4851 commit 1926974

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
@@ -1609,33 +1609,34 @@ are always available. They are listed here in alphabetical order.
16091609

16101610
.. note::
16111611

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

1617-
- When stdout is connected to a terminal (TTY), output is line-buffered and typically flushes
1617+
- When stdout is connected to a terminal (TTY), output is line-buffered and typically flushes
16181618
after the write completes.
1619-
- When stdout is redirected to a file or pipe, output may be fully buffered and not flush
1619+
- When stdout is redirected to a file or pipe, output may be fully buffered and not flush
16201620
until the buffer fills or flush is requested.
16211621

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

16261626
Example:
16271627

16281628
.. code-block:: python
1629-
16301629
from time import sleep
16311630
1632-
print("Hello\nWorld", end='') # Both lines appear together on TTY
1631+
# Whether the default end is a newline ('\\n') or any other character,
1632+
# Python performs a single write operation for the entire string.
1633+
# Therefore, newlines inside the string do not cause mid-string flushing.
1634+
print("Hello\nWorld")
16331635
sleep(3)
16341636
print("Hi there!")
16351637
1636-
.. versionchanged:: 3.3
1637-
Added the *flush* keyword argument.
1638-
1638+
.. versionchanged:: 3.3
1639+
Added the *flush* keyword argument.
16391640

16401641
.. class:: property(fget=None, fset=None, fdel=None, doc=None)
16411642

0 commit comments

Comments
 (0)