Welcome to the definitive master tutorial module for Python Iterators & Iterable Protocols. This directory features a 3-step sequential curriculumβguiding students step-by-step from fundamental iter() and next() calls and built-in container iterators, to custom classes (__iter__ / __next__), two-argument sentinel iterators, and
iterator/
βββ 01-Iterator-Fundamentals/
β βββ iterator_protocol_basics.py # iter(), next(), StopIteration exception handling & defaults
β βββ container_iterators.py # list, tuple, dict, str, and file line (TextIOWrapper) iterators
β βββ test_fundamentals.py # 6 Unit tests for iterator fundamentals & container views
βββ 02-Custom-Iterators-and-Classes/
β βββ custom_class_iterator.py # Custom AlphabetIterator & BoundedFibonacciIterator classes
β βββ infinite_and_sentinel_iter.py# Two-argument iter(callable, sentinel) & counter iterators
β βββ test_custom_iterators.py # 4 Unit tests for custom iterator classes & sentinel calls
βββ 03-Range-Iterators-and-Performance/
β βββ range_iterator_performance.py# iter(range(n)), O(1) space benchmarks, dir(range) introspection
β βββ test_range_iterators.py # 4 Unit tests for range iterators & reflection matrix
βββ dictionary_iterator_demo.py # Refactored PEP 8 dictionary view iterator demonstration
βββ file_iterator_demo.py # Refactored PEP 8 lazy file line streaming demonstration
βββ directory_iterator_demo.py # Refactored PEP 8 cross-platform directory scanner demo
βββ range_sequence_demo.py # Refactored PEP 8 range iterator sequence demonstration
βββ tuple_iterator_demo.py # Refactored PEP 8 tuple iterator demonstration
βββ custom_class_demo.py # Standardized PEP 8 custom class iterator demonstration
βββ manual_next_demo.py # Standardized PEP 8 manual next() calls demonstration
βββ iter.py # Master demonstration entrypoint script
βββ test_iterator_master.py # Master unittest suite runner executing all 14 unit tests
βββ grade.txt # Sample text file used for file stream iteration demonstrations
βββ README.md # Pedagogical overview & quickstart instructions
βββ docs.md # Technical documentation, iterator protocol hooks & version matrices
python3 iterator/iter.py# Step 1: Iterator Fundamentals
python3 iterator/01-Iterator-Fundamentals/iterator_protocol_basics.py
python3 iterator/01-Iterator-Fundamentals/container_iterators.py
# Step 2: Custom Iterators & Classes
python3 iterator/02-Custom-Iterators-and-Classes/custom_class_iterator.py
python3 iterator/02-Custom-Iterators-and-Classes/infinite_and_sentinel_iter.py
# Step 3: Range Iterators & Performance
python3 iterator/03-Range-Iterators-and-Performance/range_iterator_performance.py# Run Master Test Suite via unittest
python3 iterator/test_iterator_master.py
# Run Master Test Suite via pytest
pytest iterator/-
Iterables vs. Iterators: An Iterable defines
__iter__()returning an Iterator. An Iterator defines__iter__()(returningself) and__next__()(returning values or raisingStopIteration). -
Safe Iteration (
next(iterator, default)): Preventing unhandledStopIterationexceptions by providing default fallback values. -
Custom Iterator Protocol: Implementing custom stateful iteration logic inside user-defined classes (
AlphabetIterator,BoundedFibonacciIterator). -
Two-Argument
iter(callable, sentinel): Continuously evaluating callable objects until a sentinel boundary is reached. -
Range Iterator Performance: Benchmarking
$O(1)$ memory consumption of C-levelrange_iteratorobjects vs$O(N)$ materialized list iterators. -
Cross-Version Evolution: Detailed breakdown from Python 2.7 (legacy
next(),xrange,.iterkeys()) through Python 3.3 (yield from), 3.5 (async iterators), 3.11 (adaptive interpreter), and 3.13 (free-threaded CPython and JIT).