Using Advanced Generator Methods
In this lesson, you’ll learn about the advanced generators methods of
.close(). To practice with these new methods, you’re going to build a program that can make use of each of the three methods.
As you follow along in the lesson, you’ll learn that
yield is an expression, rather than a statement. You can use it as a statement, but you can manipulate a yielded value. You are allowed to
.send() a new value back to the generator. You’ll also handle exceptions with
.throw() and stop the generator after a given amount of digits with
00:31 So, examples of number palindromes could be 121, 111, 141, 747, and so on. Examples of words are things like level, radar, and so on. Okay. So, you don’t need to understand how this function works exactly in detail, just at a very high level. Here we’re checking if an input number has a single digit and if it does, then we skip it.
I guess we could argue whether or not a single digit is a palindrome, but they’re kind of special cases. Then here on lines 5 to 9, we are actually reversing the number and checking if the number itself is the same as its reversed version. If that is the case, we will return
True. If not, we will return
Now, this line here is quite interesting because you can see that
yield is to the right of an assignment operator. This works together with the code down here, so here we’re starting our
we’re iterating through it, we’re printing out numbers which turn out to be palindromes. Then, if they turn out to be palindromes, we are extracting their length, so the number of digits in the palindrome, and here we’re adding a leading
1 to it.
02:50 This is the first of the advanced methods I wanted to show you. What this is doing is this is sending this to our generator, and the way it does that is—remember we started the generator here—it ran until it found one palindrome.
And then this
send() is sending that value, so
1000, here in the place of
num. And that’s how
yield, in this case, is playing a double role. On the one hand, it returns a value to us, or yields a value to us. But on the other hand, it works as a kind of placeholder where we can slide a new value into it.
03:54 And there you can see it’s giving us palindromes and each time it’s finding a palindrome which is one digit longer than the previous one. At first it’s quite quick, and then the sequences become longer and longer, so you can see that it starts to slow down, but it’s still running.
Now, remember, here in
digits we are extracting the length of the palindrome which we just found. I’m going to modify this a bit. And what I’m going to do is I’m going to add a check here, whether or not we have found a palindrome which has five digits.
So, what this is doing is that the
throw() method is causing the generator to throw an exception and we’re specifying which exception we want. So I’m telling it I want a
ValueError and I want the error message to read,
"We don't like large palindromes". I’m going to save this, I will enlarge the terminal again, and I’m going to run it. Well, here at the terminal first.
So remember that generators keep state between each time they’re being called and
close() causes the generator to lose state. It sort of evaporates the way a normal function would if it hit a
return statement. So coming back to the IDE, if you’ll remember from a minute ago, I had introduced this
throw() method here.
And you can see that the generator has given us a
StopIteration exception, which is what normally happens when a generator is exhausted. So
close() caused the generator to stop and behave as if it had exhausted its sequence.
So those were the three advanced generator methods that I wanted to show you. First, there’s
send() allows you to send a value into where the generator last stopped, so where the
yield statement was.
Become a Member to join the conversation.