[docs] Document why generators don't support the context management protocol (issue 13814)

vadmium+py at gmail.com vadmium+py at gmail.com
Wed Apr 22 06:01:59 CEST 2015


https://bugs.python.org/review/13814/diff/14541/Doc/faq/design.rst
File Doc/faq/design.rst (right):

https://bugs.python.org/review/13814/diff/14541/Doc/faq/design.rst#newcode829
Doc/faq/design.rst:829: Why don't generators support the with statement?
Perhaps put quotes around the keyword so that it doesn’t choke my
English grammar parser (consistent with the existing section about
attribute assignments):

Why don't generators support the "with" statement?

https://bugs.python.org/review/13814/diff/14541/Doc/faq/design.rst#newcode832
Doc/faq/design.rst:832: Any time you use generators to create context
managers you have to add a @contextmanager decorator.
Just a technicality, but you don’t _have_ to use that decorator. I
sometimes find myself using closing(generator) directly when I want my
generator-cum-context-manager to have more than one yield step. How
about:

Generators are often used to create context managers with the
:class:`contextlib.contextmanager` decorator.

https://bugs.python.org/review/13814/diff/14541/Doc/faq/design.rst#newcode837
Doc/faq/design.rst:837: lot harder to find and fix. Automatically
closing your generator can just as easily be done with
contextlib.closing()
Please link to :func:`contextlib.closing` and end the sentence with a
full stop.

https://bugs.python.org/review/13814/


More information about the docs mailing list