Stateful Generators and Coroutines
When you use a generator in Python, it keeps track of its own internal state each time you yield a value. This means that every time you call next() on a generator, it resumes right after the last yield statement, with all local variables and control flow exactly as you left them. This property lets you build generators that remember what happened during previous iterations, making them ideal for tasks that need to maintain a running state.
You can take this idea further by using the send() method. Unlike next(), which simply resumes the generator and retrieves the next yielded value, send() allows you to pass a value back into the generator at the point where itβs paused. The generator receives this value as the result of the yield expression. This makes it possible to build simple coroutines: generator functions that can both yield values outward and accept values from the outside world, reacting to them and updating their state accordingly.
123456789101112def running_total(): total = 0 while True: value = yield total if value is not None: total += value gen = running_total() print(next(gen)) # Output: 0 print(gen.send(5)) # Output: 5 print(gen.send(3)) # Output: 8 print(gen.send(-2)) # Output: 6
In this example, the running_total generator keeps a running sum of all values sent to it. Each time you call send() with a number, the generator adds that number to its total and yields the updated sum. The generator's stateβthe value of totalβis preserved between calls, and you can interactively update it by sending in new values.
1. What is the primary purpose of the send() method in Python generators?
2. Which statements are true about next() and send() when working with generators?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 6.67
Stateful Generators and Coroutines
Swipe to show menu
When you use a generator in Python, it keeps track of its own internal state each time you yield a value. This means that every time you call next() on a generator, it resumes right after the last yield statement, with all local variables and control flow exactly as you left them. This property lets you build generators that remember what happened during previous iterations, making them ideal for tasks that need to maintain a running state.
You can take this idea further by using the send() method. Unlike next(), which simply resumes the generator and retrieves the next yielded value, send() allows you to pass a value back into the generator at the point where itβs paused. The generator receives this value as the result of the yield expression. This makes it possible to build simple coroutines: generator functions that can both yield values outward and accept values from the outside world, reacting to them and updating their state accordingly.
123456789101112def running_total(): total = 0 while True: value = yield total if value is not None: total += value gen = running_total() print(next(gen)) # Output: 0 print(gen.send(5)) # Output: 5 print(gen.send(3)) # Output: 8 print(gen.send(-2)) # Output: 6
In this example, the running_total generator keeps a running sum of all values sent to it. Each time you call send() with a number, the generator adds that number to its total and yields the updated sum. The generator's stateβthe value of totalβis preserved between calls, and you can interactively update it by sending in new values.
1. What is the primary purpose of the send() method in Python generators?
2. Which statements are true about next() and send() when working with generators?
Thanks for your feedback!