Generators are powerful tools in Python for handling large data streams efficiently. Here's a breakdown of advanced concepts:
1. Generator Expressions
A concise way to create generators using a syntax similar to list comprehensions.
numbers = (x**2 for x in range(10))
for num in numbers:
print(num)
2. Yield vs Return
yield
pauses the function and saves its state, resuming from where it left offreturn
exits the function entirely
Example:
def count_up_to(n):
count = 0
while count < n:
yield count
count += 1
3. Iterators Protocol
Generators automatically implement the iterator protocol via __iter__()
and __next__()
methods.
4. Advanced Use Cases
- Chaining generators:
def chain(*iterables): for i in iterables: yield from i
- Infinite sequences:
def infinite_sequence(): num = 0 while True: yield num num += 1
5. Best Practices
- Avoid using generators for small datasets
- Use
yield from
to delegate to sub-generators - Combine with
itertools
for enhanced functionality
For more examples, check our Python Generator Basics Tutorial. 📚