Welcome to the Context Managers Tutorial Module in Python. This directory provides clear, pedagogical demonstrations of class-based context managers, generator-based context managers (@contextmanager), directory switching patterns, and comprehensive unit tests.
- Overview & Architecture
- Module Index
importvsfrom ... import ...Namespace Mechanics- Cross-Version Evolution (Python 2.7 to Python 3.13)
- Range Sequence Iterators & Performance Notes
dir(range)Reflection Matrix- Running Unit Tests
Context managers handle resource setup and teardown automatically using Python's with statement:
with EXPRESSION as TARGET:
SUITE__enter__(self): Initializes the resource (e.g., opening a file or changing working directory) and returns a reference.__exit__(self, exc_type, exc_val, exc_tb): Guarantees resource release (e.g., closing file stream or restoring working directory), even when runtime errors occur.
| Module Filename | Description & Purpose |
|---|---|
class_context_manager.py |
Class-based context manager (OpenTextFile) implementing __enter__ and __exit__. |
generator_context_manager.py |
Generator context manager (open_text_file) using contextlib.contextmanager. |
context_manager_directory_change.py |
Directory switching context manager (change_directory) preserving CWD state. |
dir_first/ |
Single-resource context management, stream safety, and unit test suite. |
dir_second/ |
Multi-resource dynamic ExitStack context management and unit test suite. |
test_context_managers.py |
Automated unittest suite covering all context manager modules in this directory. |
import os: Imports the entireosmodule object into the current namespace. Functions are called with explicit namespace qualification:os.getcwd().from contextlib import contextmanager: Imports specific symbols (contextmanager) directly into the local namespace, enabling direct call syntax@contextmanager.
| Python Release | Context Manager & Syntax Feature | Architectural Difference |
|---|---|---|
| Python 2.7 | Manual try...finally resource cleanup |
Requires explicit fh.close() in finally blocks; range() evaluates full lists in memory. |
| Python 3.3 |
contextlib.ExitStack introduced |
Dynamic resource management introduced. range converted to |
| Python 3.10 | Parenthesized context managers |
with (A() as a, B() as b): syntax introduced. |
| Python 3.13 | Zero-cost inline exception handling | Specialized CPython bytecode (BEFORE_WITH, WITH_EXCEPT_START). |
# Legacy Python 2.7 (Manual cleanup):
fh = open("myfile.txt", "w")
try:
fh.write("Legacy write\n")
finally:
fh.close()
# Modern Python 3.13 (Context Manager):
with open("myfile.txt", "w", encoding="utf-8") as fh:
fh.write("Modern write\n")In Python 3.3+, range() generates numbers on demand with
import sys
# O(1) Memory footprint:
r_iter = iter(range(1_000_000))
print(f"range iterator memory: {sys.getsizeof(r_iter)} bytes") # ~48 bytes
# O(N) Materialized list memory:
r_list = list(range(1_000_000))
print(f"Materialized list memory: {sys.getsizeof(r_list)} bytes") # ~8 MBInspecting dir(range) displays the sequence attributes and public methods:
r = range(10, 100, 5)
print("Start:", r.start) # 10
print("Stop :", r.stop) # 100
print("Step :", r.step) # 5
print("Index of 25:", r.index(25)) # 3
print("Public Members:", [m for m in dir(r) if not m.startswith("__")])
# ['count', 'index', 'start', 'step', 'stop']Execute the unit test suite from the repository root:
python3 -m unittest discover -s context_managers -p "test_*.py"