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.
This commit is contained in:
Terry Jan Reedy 2021-06-21 17:23:29 -04:00 committed by GitHub
parent 355f5dd36a
commit 51f45d085d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View file

@ -708,6 +708,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?
--------------------------------------------------------------

View file

@ -0,0 +1 @@
In the Design FAQ, answer "Why don't generators support the with statement?"