Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today we're discussing coroutines, which enhance the behavior of generators by allowing two-way communication. Can anyone summarize what a generator does?
A generator yields values one at a time until it's done.
Right! Now, how do you think coroutines expand on this functionality?
I guess they can take values back, like sending input to the generator?
Exactly! That's a key aspect of coroutines. They can pause execution and receive values, which makes them quite powerful.
How do we actually send values to a generator?
Great question! We’ll cover that in detail with an example. For now, remember that send() is your method for sending values back into a coroutine.
To wrap up, coroutines enable two-way data flows in your code. We use yield to send data out and send() to bring data back in.
Let's look at a practical example of how send() works in a coroutine. Here's a simple accumulator function that takes in values, sums them up, and yields the total.
Can we see how that works in code?
"Sure! Here's the code snippet:
To summarize, we discussed how coroutines allow not just yielding but also receiving values. This two-way communication is essential in scenarios like asynchronous programming.
So it's like a thread of communication between our program and the generator?
Absolutely! The ability to pause and resume execution with yield and send() opens a variety of programming possibilities.
Are coroutines more efficient than using traditional function calls?
That's a great point. Coroutines offer memory efficiency and the ability to manage state without deep call stacks, which can boost performance. Always remember, though, to use them wisely!
When you yield, you pause the game, send a value, and resume the same.
Imagine a mailman (coroutine) who takes letters (values). He pauses to deliver them and waits to receive replies (using send) before going back to work.
Y for Yield, S for Send — remember, they both work as a helpful trend!
C.Y.S = Coroutines, Yield, Send. A simple reminder of how they relate!
Term
What is a coroutine?
Click to flip
Term
How do coroutines send values to themselves?
Click to flip
Term
What method starts the execution of a coroutine?
Click to flip
Term
What do you use to yield output from a coroutine?
Click to flip
A special type of function that can pause and resume its execution, allowing for two-way communication with the caller.
A keyword that allows a function to return a value and pause its execution for later resumption.
A method used to send values into a coroutine, resuming its execution at the point of the last yield.
A common use case for coroutines where values are summed up and returned to the caller.
This section introduces coroutines, focusing on their ability to receive values via generators, highlighting the interaction between yield and send methods.
In this section, we explore the concept of coroutines in Python, which allow two-way communication with generators. This involves understanding how to send values back to a generator using the send() method while managing the state of the generator.
Coroutines offer an extension to the typical generator functionality in Python, allowing for not just yielding values but also receiving values. A coroutine is a function that can pause its execution, yield control back to the caller, and then resume from where it paused. This behavior enables two-way communication between a generator and the caller.
The following function serves as an example of a coroutine:
def accumulator():
total = 0
while True:
value = yield total # Yield the total and wait for a value input
if value is None:
break
total += valueIn this accumulator function:
yield statement sends the current total back to the caller. The execution is paused until a new value is sent to the generator.send(value) method resumes the generator's execution and sends the specified value into it. On the first call, using next() or send(None) starts the generator.acc = accumulator()
print(next(acc)) # Start the generator, outputs 0
print(acc.send(10)) # Send 10, output 10
print(acc.send(5)) # Send 5, output 15
acc.send(None) # Stop the generatorThis output illustrates the coroutine receiving values effectively and accumulating them until told to stop, demonstrating the concept of coroutines as powerful tools for managing state and flow in Python applications.