Generators
Functions that yield values one at a time, producing sequences lazily and saving memory.
Intermediate
def sample(sensor, n):
for _ in range(n):
yield sensor.read()
for value in sample(adc, 1000):
process(value)
yield pauses the function and resumes it on the next request, so even infinite streams work without storing everything in memory. Generators are ideal for reading large files line by line or processing a stream of sensor data.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.