diff options
author | Brett Cannon <brett@python.org> | 2016-04-08 19:15:27 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2016-04-08 19:15:27 (GMT) |
commit | 9e080e0e741dd70cf86500f848eee19cf8b29efa (patch) | |
tree | 7d5a4fa6c9279df0c3c3bddd2d041619a6862fa7 /Doc/library/contextlib.rst | |
parent | c5b5ba9bdafaf2542ac2e6939f025a01a10549c2 (diff) | |
download | cpython-9e080e0e741dd70cf86500f848eee19cf8b29efa.zip cpython-9e080e0e741dd70cf86500f848eee19cf8b29efa.tar.gz cpython-9e080e0e741dd70cf86500f848eee19cf8b29efa.tar.bz2 |
Issue #25609: Introduce contextlib.AbstractContextManager and
typing.ContextManager.
Diffstat (limited to 'Doc/library/contextlib.rst')
-rw-r--r-- | Doc/library/contextlib.rst | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst index c112241..7876e7a 100644 --- a/Doc/library/contextlib.rst +++ b/Doc/library/contextlib.rst @@ -18,6 +18,18 @@ Utilities Functions and classes provided: +.. class:: AbstractContextManager + + An abstract base class for classes that implement + :meth:`object.__enter__` and :meth:`object.__exit__`. A default + implementation for :meth:`object.__enter__` is provided which returns + ``self`` while :meth:`object.__exit__` is an abstract method which by default + returns ``None``. See also the definition of :ref:`typecontextmanager`. + + .. versionadded:: 3.6 + + + .. decorator:: contextmanager This function is a :term:`decorator` that can be used to define a factory @@ -447,9 +459,9 @@ Here's an example of doing this for a context manager that accepts resource acquisition and release functions, along with an optional validation function, and maps them to the context management protocol:: - from contextlib import contextmanager, ExitStack + from contextlib import contextmanager, AbstractContextManager, ExitStack - class ResourceManager: + class ResourceManager(AbstractContextManager): def __init__(self, acquire_resource, release_resource, check_resource_ok=None): self.acquire_resource = acquire_resource |