summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTerry Jan Reedy <tjreedy@udel.edu>2021-06-21 21:23:29 (GMT)
committerGitHub <noreply@github.com>2021-06-21 21:23:29 (GMT)
commit51f45d085dad3b708f6fe166af517aba69e7e9f7 (patch)
tree7919b4b99f22ea2a207cdc5048016cfcdb2db63a
parent355f5dd36a0f53175517f35798aa874564d1113a (diff)
downloadcpython-51f45d085dad3b708f6fe166af517aba69e7e9f7.zip
cpython-51f45d085dad3b708f6fe166af517aba69e7e9f7.tar.gz
cpython-51f45d085dad3b708f6fe166af517aba69e7e9f7.tar.bz2
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.
-rw-r--r--Doc/faq/design.rst9
-rw-r--r--Misc/NEWS.d/next/Documentation/2021-06-21-15-46-32.bpo-13814.LDcslu.rst1
2 files changed, 10 insertions, 0 deletions
diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst
index 68570b3..720b1e4 100644
--- a/Doc/faq/design.rst
+++ b/Doc/faq/design.rst
@@ -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?
--------------------------------------------------------------
diff --git a/Misc/NEWS.d/next/Documentation/2021-06-21-15-46-32.bpo-13814.LDcslu.rst b/Misc/NEWS.d/next/Documentation/2021-06-21-15-46-32.bpo-13814.LDcslu.rst
new file mode 100644
index 0000000..db0c6d6
--- /dev/null
+++ b/Misc/NEWS.d/next/Documentation/2021-06-21-15-46-32.bpo-13814.LDcslu.rst
@@ -0,0 +1 @@
+In the Design FAQ, answer "Why don't generators support the with statement?"