diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-06-21 22:02:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-21 22:02:43 (GMT) |
commit | 1e16217204c0e8e595c4d1e869c81899bfe3376b (patch) | |
tree | 42b5dd9e89d0ecb4ede2f5662b8ef6330202d87a | |
parent | 20a1495b8a93596b322ea19bb09008db036eb7e6 (diff) | |
download | cpython-1e16217204c0e8e595c4d1e869c81899bfe3376b.zip cpython-1e16217204c0e8e595c4d1e869c81899bfe3376b.tar.gz cpython-1e16217204c0e8e595c4d1e869c81899bfe3376b.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.
(cherry picked from commit 51f45d085dad3b708f6fe166af517aba69e7e9f7)
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
-rw-r--r-- | Doc/faq/design.rst | 9 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Documentation/2021-06-21-15-46-32.bpo-13814.LDcslu.rst | 1 |
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?" |