Python Generators: Lazy Perfection
Python Generators: The Lazy Programmers Dream
Ever tried list(range(100_000_000))? Your fans spin up, memory spikes, Chrome tabs crash, cursor turns into beach ball. You hit Ctrl+C and pretend it never happened.
Problem: lists are too honest. They cram everything into memory. What if something LOOKS like a list but is LAZY - only computes when asked, sleeps otherwise?
Enter: Generators.
Generator vs List
# Honest list: compute all, store all
big_list = [x for x in range(100_000_000)]
# BOOM memory
# Lazy gen: reserve spot, give one at a time
big_gen = (x for x in range(100_000_000))
# Rock solid memoryList comprehension = []. Generator expression = (). Small difference, huge impact.
yield: The Soul of Generators
def countdown(n):
while n > 0:
yield n # pause + return
n -= 1
for num in countdown(5):
print(num)
# 5 4 3 2 1Lazy Evaluation: compute one, give one. Never produce ahead of time.
Killer Use Case: Read Huge Files
def read_large(path):
with open(path) as f:
for line in f:
yield line.strip()10GB log file? Process millions of lines with KBs of memory. Dimensional reduction.
Infinite Sequences
def fibonacci():
a,b = 0,1
while True:
yield a
a,b = b, a+b
fib = fibonacci()
for _ in range(10):
print(next(fib)) # 0 1 1 2 3 5 8 13 21 34When to Use What?
Small data (thousands) = list. Large data (millions) = generator. Need random access = list. Stream processing = generator.
Summary: Be a Lazy Genius
Generator philosophy: compute when needed, not before. Like ordering takeout - you dont cook a years worth of meals and freeze them (list). You order when hungry, eat one meal (generator).
Python generators: be an elegant lazy person. Learn them. Next time, for sure!