Skip to content

Commit bee66d3

Browse files
bpo-41147: [doc] contextlib.redirect_stdout() provides the new stream as context var (GH-21199) (GH-26380)
(cherry picked from commit 46db39d) Co-authored-by: Peter Law <[email protected]>
1 parent e7078ee commit bee66d3

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
@@ -236,10 +236,11 @@ Functions and classes provided:
236236

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

241-
f = io.StringIO()
242-
with redirect_stdout(f):
243+
with redirect_stdout(io.StringIO()) as f:
243244
help(pow)
244245
s = f.getvalue()
245246

0 commit comments

Comments
 (0)