Generators & yield Keyword Mechanics
1Concept
Generators are functions that preserve state across invocations using `yield`. When executed, they return a generator object. Code runs lazily up to the next `yield` statement, suspending the call frame in memory without consuming memory for entire collections.
2Architecture Diagram
def gen(): yield 1 ---> [ Suspends Frame ] ---> Next request resumes execution ---> yield 2
3Code Example
Python 3.12
import sys
def infinite_log_stream(limit: int):
for i in range(1, limit + 1):
yield f"[LOG-{i:04d}] Process Heartbeat Active"
stream = infinite_log_stream(1_000_000)
print(f"Generator Memory Footprint: {sys.getsizeof(stream)} bytes (Zero Heap bloat!)")
print(f"First item: {next(stream)}")
print(f"Second item: {next(stream)}")4Expected Output
Generator Memory Footprint: 200 bytes (Zero Heap bloat!) First item: [LOG-0001] Process Heartbeat Active Second item: [LOG-0002] Process Heartbeat Active
5Key Takeaways
- ✓Generators provide O(1) memory streaming for gigabyte-scale datasets.
- ✓A generator expression `(x * 2 for x in data)` is the lazy equivalent of list comprehension.
- ✓Once consumed, a generator cannot be restarted; a new generator instance must be created.