Skip to content
Snippets Groups Projects
Unverified Commit d881002f authored by Miss Islington (bot)'s avatar Miss Islington (bot) Committed by GitHub
Browse files

bpo-13814: Explain why generators are not context managers (GH-26835)


Put entry in Design FAQ after a question about a context manager for assignment.
Original patch by Aidan Lowe.
(cherry picked from commit 51f45d08)

Co-authored-by: default avatarTerry Jan Reedy <tjreedy@udel.edu>
parent 67b3a999
Branches
Tags
No related merge requests found
......@@ -709,6 +709,15 @@ bindings are resolved at run-time in Python, and the second version only needs
to perform the resolution once.
Why don't generators support the with statement?
------------------------------------------------
For technical reasons, a generator used directly as a context manager
would not work correctly. When, as is most common, a generator is used as
an iterator run to completion, no closing is needed. When it is, wrap
it as "contextlib.closing(generator)" in the 'with' statment.
Why are colons required for the if/while/def/class statements?
--------------------------------------------------------------
......
In the Design FAQ, answer "Why don't generators support the with statement?"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment