From Martin Heinz
Some Cools Things contextlib Can Do
- Trigger an object to close with
contextlib.closing - Suppress warnings with
contextlib.suppress. Equivalent to:
try:
do_something()
except RuntimeError:
pass- Add
__enter__and__exit__functionality to functions via thecontextlib.ContextDecoratormixin / decorator
There Are Three Types of Context Managers
- Single-use
- Reusable
- Reentrant—Reusable, and can be nested 🤯