3.3 Iterators and Generators Part 1

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *

Already have an account? Sign In »

2 hours 30 minutes
Video Transcription
Hello and welcome back to Intermediate Python here on Cyber Eri On demand, I as always I'm your instructor, Joe Perry. And in today's video, we're gonna be going over a lesson too, which is generators and generators. And in this in this lesson, we're actually gonna only have two main learning objectives. We're going to learn what it aerators and generators are, and we're gonna implement both of those
this'll Essen is actually gonna be two videos long. The 1st 1 is just gonna be about generators, and then the 2nd 1 will be about generators.
So to do this to get into this, we're gonna jump right in or b m here and start talking a little bit. The first thing to understand about generators is to remember back to our class video and remember that everything in python is actually under the hood. Implemented as a class, even strings and imagers, what you're using the python strings and the python editors are implemented as classes,
which, of course, means that generator's probably are as well,
so the best way to really understand it aerators and how they're used because you've actually interact with generators. A few top a few times already, but the best way to understand them and be able to apply the Mr Actually build out in generator class. So that's what we're gonna be doing here. And we're just gonna go ahead and vim and we'll call it in our demo dot pie
Inter shebang line here
and we're going to construct a class and this we're just gonna call Inter demo.
And because we're not inheriting from anything we don't have to use parentheses were just calling inter demo. And as always, the first thing we're going to do in any class is initialized.
And here we're going to say, self dots
and we're going to give it the alphabet.
I j k l I know that you probably know the alphabet, but it's not great to have dead air, so I'm gonna go ahead and keep muttering mumbling along as I work There we go. Do you be cool? Got it right. And then we're going to create an indexing variable that we're gonna call I and that'll make a little bit more sense here in just a second.
So just like any glass were just initializing Here we have two attributes that we're going to be a signing right at the beginning.
And then we're going to create one of the two methods that you must have for a class to be an operator. And they're like a ZAY just said there are only two methods that you must have Your class could do all sorts of other stuff so long as it has these two methods, the first of which
is going to be better.
And there is an extremely simple method. All it's used for is actually to return
the self object. And this is done for four loops, the mechanisms under which you actually generate through something. You do that with four loops, and you kind of, you know, you have to have access to this object. You have to be able to understand what the actual class instance you're working with is that's all under the hood being done by that loop.
So it is the function we need to use to make that possible. The second function of the second method that we want to implement here
is the next method, and the next method is gonna be the real workhorse here because that's gonna be the one that actually provides the return whenever it calls next. Whenever your generator is actually being implemented. And what we're gonna do here is we're going to return.
We're going to say letter
equals self dot alphabet
we'll call it I and we will use our index variable here. Now, you know that I didn't do self, not letter. The simple reason for that is because this variable is not going to be access outside of this method, so we don't really have to worry about it too much. So let her equal self dot alphabet with the index I and then we're going to go ahead and I Plus he
self die
sofa I plus
and then return letter.
So let's see if we've done this correctly. Uh, if we have, we have to.
All right. Everything's going crazy. You're Hang on a second.
There you go.
Them inter dammit, a pie. So if this is working correctly, we should be able to create a classic instance that we're gonna call I D.
And then we should be able to four I range
not in range for I in i d
Sorry. Trip myself off their print. I d.
Now, I didn't really give a great definition of it aerators of beginning this video because I wanted to kind of have this moment, this damage seen moment right here
where you realize that in generator is just something that can be iterated over, which is slightly self referential. Definition and generator is anything that has multiple objects that could be addressed individually by four loops or by an iterating, Lupin iterating function and python.
So, really, the core definition is an it aerator is any class which has those two methods theater in the next.
But the easy way to understand it is if it can be the, uh, the item after in in a four loop that makes him that means that it is an operator.
So we can see here print i d four i an I d. We're going to print I
and we can see if this actually ran. If this worked out correctly
and we'll say
and let's see if we got a ride on the first try.
Sure enough we did. Now there's an error message in here. The bottom. We're gonna dress in just a second, but Sure enough, we printed out each letter of the alphabet. A jersey.
I said, we get to the error in just a minute. And sure enough, that minute has already passed. Its a shorter minute that you might have expected. Here we go.
Now, what happened was we got an index error, and the reason we got an index error is because we kept running through this until we got out. We ran out of right. It was remember that a four loop will run until such time as it reaches a thing. End of the condition that it's working with. So in this case, so long as I d keeps serving up
letters this for Luke will continue filling out the eye variable.
And you'll notice here that even though we're going through the alphabet, we're using all these variables. We're doing all this correctly. We don't have any boundary conditions. We never have a moment where we say, Oh, hey,
ignore that. We never have a moment where we say, Oh, hey, we've gotten all the letters were done. We have no more letters to print out. It just keeps trying, which is why we're getting that air. So one of the things you need to do and this is kind of a useful lead in, uh,
for for exceptions, video that's coming up here in just a minute is what you need to do in order to make an irritable worker and generator. Actually, work is you need to have a condition where you identify that you've reached the end of the objects or the end of the items that you can serve and you give that information back. And that's done with a special exception or a special sort of error message
that is, stop it oration
and all we have to do for that is, we can say,
because we know that we have 26 letters in the alphabet. Weaken, say, if eyes greater than 26 25 actually, because we started zero if I was greater than 25
braise stop
Let's see if this works
Sure enough, it doesn't because I forgot to use self on that I, which I keep doing. Let's try this again
and it hurt.
Okay, so we still have the wrong string index, which means that we don't have the right check in that eye, and that's okay. We're gonna go ahead and reopen this
if self that is greater than 25.
And the reason that's happening is because of the fact that we're addressing past the last item. So we should say, If I equals 25 if I has already gotten to the last item
and see if this one works,
double equals that is, I mentioned before I mention again. That is one of the most common gouaches, and Python programming is the double equal versus single equal.
Unfortunately, Python is nice enough to tell you that there is a sin tax problem and you're doing something wrong. So let's try this hopefully one last time. There we go. Fantastic. So you can see here that we now print a through Z without ever throwing up this error, and you'll note that I'm still raising the exception. I've mentioned exceptions are error messages, and you'll understand
once we get to our exceptions video a little bit better. Why that doesn't create any error messages and why everything works
by using that. So that's the basics of how you can implement and generator, and that is what it aerators are under the hood, there just classes with theater and the next functions that help python that allow Python to interpret all of their values or all of their characters.
Now, one of the really interesting things you might do for an ID aerator like this is instead of using a specific set of items like this string that's actually already its own it aerator,
you might have a function done here when they call next. It actually produces some work, so you might produce a random number, in which case you don't ever have to stop federation, because you can always produce a new random number. So one of the cool things about sort of wrap are rolling. Your own generator is that you can have it do all sorts of sort of unusual behavior. You can
really make use of pythons for Lupin while loops and all the work that's done under the hood. To understand how to interpret these it aerators
without ever actually having to, you know, behave quite how your how you would normally think of ah list or a dictionary or a string to behave.
This is gonna be the end of it triggers now we understand what they are and how they're implemented, and that is allowing us to better understand how they're used in what you can do with them. Basic. As I said, basically anything that can go right here on your four loop counts as an eatery because that's being iterated over.
So it's gonna be the end of this video. Hopefully, you learned a lot, and hopefully we get you back next video where we're gonna be talking all about
generators. Thank you for watching. I have been your instructor, Joe Perry, and you're watching Intermediate Python on Cyber Eri on demand.
Up Next