A dependency-free Python CLI with short, labeled examples of context manager behavior. Follow entry and exit, exception propagation, opt-in suppression, generator-based managers, and ExitStack cleanup order.
- 🚪 Demonstrates class-based enter and exit methods.
⚠️ Shows how exceptions propagate unless exit explicitly suppresses them.- 🧵 Demonstrates contextlib.contextmanager with try/finally cleanup.
- 📚 Shows ExitStack callbacks running in last-in-first-out order.
Run the CLI to see each example printed in sequence. No external files or services are used.
python -m pip install -e .
context-manager-labRun the unit checks with python -m unittest discover -s tests.
- Python 3.10+ — contextlib, argparse, and unittest; no third-party dependencies.
- License — MIT
python-context-manager-lab/
├── assets/mark.svg
├── src/context_manager_lab/
│ ├── __init__.py
│ └── cli.py
├── tests/test_cli.py
├── .gitignore
├── LICENSE
├── pyproject.toml
└── README.md
- Add async context manager examples.
- Add nested resource cleanup scenarios.
- Add examples for contextlib.ExitStack exception handling.
Issues and pull requests are welcome. Keep examples deterministic and add tests for any new suppression or cleanup behavior.
Released under the MIT License.