mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +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.
This commit is contained in:
parent
355f5dd36a
commit
51f45d085d
2 changed files with 10 additions and 0 deletions
|
@ -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?
|
||||
--------------------------------------------------------------
|
||||
|
||||
|
|
|
@ -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