mirror of
https://github.com/python/cpython.git
synced 2025-10-04 06:06:44 +00:00
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 51f45d085d
)
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
This commit is contained in:
parent
67b3a99953
commit
d881002fbd
2 changed files with 10 additions and 0 deletions
|
@ -709,6 +709,15 @@ bindings are resolved at run-time in Python, and the second version only needs
|
||||||
to perform the resolution once.
|
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?
|
Why are colons required for the if/while/def/class statements?
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
In the Design FAQ, answer "Why don't generators support the with statement?"
|
Loading…
Add table
Add a link
Reference in a new issue