Context Managers
Todo
- ELI5: Context managers
- Examples you may have encountered
with open() as f:
with sqlalchemy.Session() as sess:
- Writing a context manager function with
@contextmanager
- Writing a context manager class
- The
__enter__()
and__exit__()
methods - Handling exceptions
- The
Extra reading
- Python docs: contextlib
- RealPython: Context Managers and Python's with Statement
- PythonTutorial: Python context managers
- LearnDataSci.com: Context managers in Python using the "with" statement
- FreeCodeCamp: Context Managers in Python
- Medium: Demystifying Python Context Managers: A Comprehensive Guide