Generator Basics
What is a Generator?
In Python, a generator is a special type of function that returns an iterator object which can be iterated over. Unlike regular functions that return a complete result at once, generators use the yield
keyword to produce a series of values over time, making them memory-efficient and ideal for handling large datasets.
Key Characteristics of Generators
Generators have several unique characteristics that set them apart from traditional functions:
Characteristic |
Description |
Lazy Evaluation |
Values are generated on-the-fly, only when requested |
Memory Efficiency |
Generates values one at a time, reducing memory consumption |
Iteration Support |
Can be used directly in for loops and comprehensions |
Creating Generators
Generator Functions
def simple_generator():
yield 1
yield 2
yield 3
## Using the generator
gen = simple_generator()
for value in gen:
print(value)
Generator Expressions
## Generator expression
squared_gen = (x**2 for x in range(5))
for value in squared_gen:
print(value)
Generator Workflow
graph TD
A[Generator Function] --> B{yield Keyword}
B --> C[Pause Execution]
C --> D[Return Value]
D --> E[Resume Execution]
E --> F[Continue Processing]
Advanced Generator Techniques
Generator Chaining
def count_generator(n):
for i in range(n):
yield i
def squared_generator(gen):
for value in gen:
yield value ** 2
## Chaining generators
result = squared_generator(count_generator(5))
list(result) ## [0, 1, 4, 9, 16]
Use Cases
Generators are particularly useful in scenarios involving:
- Large datasets
- Infinite sequences
- Memory-constrained environments
- Data processing pipelines
Generators provide significant memory advantages by generating values on-demand, making them an excellent choice for LabEx data science and engineering workflows.