Skip to content

Commit 46db39d

Browse files
authored
bpo-41147: [doc] contextlib.redirect_stdout() provides the new stream as context var (GH-21199)
1 parent 156699b commit 46db39d

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Doc/library/contextlib.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,11 @@ Functions and classes provided:
312312

313313
For example, the output of :func:`help` normally is sent to *sys.stdout*.
314314
You can capture that output in a string by redirecting the output to an
315-
:class:`io.StringIO` object::
315+
:class:`io.StringIO` object. The replacement stream is returned from the
316+
``__enter__`` method and so is available as the target of the
317+
:keyword:`with` statement::
316318

317-
f = io.StringIO()
318-
with redirect_stdout(f):
319+
with redirect_stdout(io.StringIO()) as f:
319320
help(pow)
320321
s = f.getvalue()
321322

0 commit comments

Comments
 (0)