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 going to explore the yield keyword in Python. Can anyone explain what a generator is?
A generator is a function that can yield values, right?
Exactly! When we use yield, we pause the function, allowing Python to return a value. For instance, with yield 1, we can start the function and then use next() to retrieve the value one at a time. This is also memory efficient.
How does it remember its state?
Great question! The generator remembers its state by saving local variables. Each time you call next(), it resumes from where it last yielded. Think of it as a bookmark in your function. Let's take a look at an example I wrote.
So, if I call next() after the last yield, what happens?
That will raise a StopIteration exception. This is an important part of using generators because it tells you that there are no more values to yield. In summary, using yield simplifies creating iterators and conserves memory!
Now, let's talk about yield from. Can anyone share their thoughts on why we would need it?
It seems like it could simplify nested loops or multiple yields?
Absolutely correct! yield from allows you to delegate part of a generator’s work to another generator, making your code cleaner. Here’s an example:
In this code, yield from allows generator1 to yield values from a list and a generator expression. It saves us from having to write additional loops.
So we can use it with both lists and other generators?
Yes! That flexibility is one of yield from's strengths. It helps keep your code DRY—don't repeat yourself. Can anyone tell me how that might improve maintenance?
If it's shorter and clearer, future updates should be easier!
Exactly! By using yield from, we build more maintainable code that’s easier to understand.
When you need to yield, don't let your function seal, pause to reveal what you can feel.
Imagine a traveler who stops at each town (yield) before continuing on their journey, finding new wonders along the way (yield from).
Remember Y for Yield (Yes, I pause) and Yd for Yield from (Yes, delegate).
YF: Yield First, Yield from. Use this when you see the term generator!
Term
What does yield do?
Click to flip
Term
What is yield from used for?
Click to flip
A keyword in Python that suspends the execution of a function, allowing it to return a value and resume later.
A keyword that delegates part of a generator’s operations to another generator or iterable, simplifying nested loops.
A special type of iterator defined by a function that yields values one at a time.
An exception raised in Python when there are no more items from a generator.
This section introduces the yield and yield from keywords, explaining their functionality in managing generator behavior in Python.
In this section, we explore how the yield keyword suspends function execution, allowing the generator to return a value and resume later. We also look at yield from, which simplifies the process of delegating part of a generator's operations to another iterable, making code cleaner and easier to understand.
In this section of the chapter, we delve into two essential keywords in Python's generator functionality: yield and yield from.
The keyword yield temporarily suspends the execution of a function, allowing it to return a value. This means that rather than terminating the function, it can be resumed later from the point it left off. An example illustrates this core functionality:
def simple_gen():
yield 1
yield 2
yield 3
gen = simple_gen()
print(next(gen)) # 1
print(next(gen)) # 2
print(next(gen)) # 3In this example, each call to next(gen) resumes execution until the next yield statement. If you call next() again after the last yield, it will raise a StopIteration exception.
Introduced in Python 3.3, yield from allows a generator to delegate part of its operations to another generator or iterable. This can reduce the complexity of nested loops. Here’s an illustrative example:
def generator1():
yield from [1, 2, 3]
yield from (x*x for x in range(4))
for value in generator1():
print(value)This code produces a sequence of numbers from two different sources without explicitly looping through each one.
Together, yield and yield from elevate the flexibility and power of generator functions, helping to simplify code and enhance readability. Understanding these constructs is pivotal for writing clean and efficient Python code.