On this page
contextlib.AbstractContextManager
Prev: contextlib.AbstractAsyncContextManager | Up: contextlib | Next: contextlib.aclosing
Rel: contextlib.AbstractAsyncContextManager, contextlib.aclosing, contextlib.asynccontextmanager, contextlib.AsyncExitStack
Ref: docs.python.org — contextlib.AbstractContextManager
Tags: #public #python3 #stdlib #contextlib
Import
import contextlib
from contextlib import AbstractContextManager
Signature & Overview
contextlib.AbstractContextManager()
An abstract base class for context managers.
Usage
from contextlib import AbstractContextManager
class Resource(AbstractContextManager):
def __init__(self) -> None:
self.closed = False
def __exit__(self, exc_type, exc, tb):
self.closed = True
return False
r = Resource()
with r as active:
assert active is r and not r.closed
assert r.closed is True
Gotchas
- Return
False(orNone) from__exit__unless suppressing exceptions: ReturningTruesilently swallows any exception raised inside thewithblock. - Supports
isinstance(obj, AbstractContextManager): Matches any class implementing both__enter__and__exit__.
REPL Inspect
from contextlib import AbstractContextManager
help(AbstractContextManager)
Linked from
- tuts/python3/stdlib_module/contextlib
- tuts/python3/stdlib_module/contextlib/AbstractAsyncContextManager
- tuts/python3/stdlib_module/contextlib/aclosing
- tuts/python3/stdlib_module/contextlib/asynccontextmanager
- tuts/python3/stdlib_module/contextlib/AsyncExitStack
- tuts/python3/stdlib_module/contextlib/chdir
- tuts/python3/stdlib_module/contextlib/closing
- tuts/python3/stdlib_module/contextlib/ContextDecorator
- tuts/python3/stdlib_module/contextlib/contextmanager
- tuts/python3/stdlib_module/contextlib/ExitStack
- tuts/python3/stdlib_module/contextlib/nullcontext
- tuts/python3/stdlib_module/contextlib/redirect_stderr
- tuts/python3/stdlib_module/contextlib/redirect_stdout
- tuts/python3/stdlib_module/contextlib/suppress
Related notes
- tuts/python3/stdlib_module/contextlib/AbstractAsyncContextManager Linked here; links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/aclosing Linked here; links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/asynccontextmanager Linked here; links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/AsyncExitStack Linked here; links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/contextlib/chdir links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/closing links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/ContextDecorator links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/contextmanager links to this page; shared tags: #contextlib, #stdlib
- tuts/python3/stdlib_module/contextlib/ExitStack links to this page; shared tags: #contextlib, #stdlib