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 *
or
Already have an account? Sign In »

Video Transcription
00:00
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
00:20
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.
00:27
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,
00:44
which, of course, means that generator's probably are as well,
00:47
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
01:03
Inter shebang line here
01:07
and we're going to construct a class and this we're just gonna call Inter demo.
01:12
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.
01:23
And here we're going to say, self dots
01:26
alphabet,
01:29
and we're going to give it the alphabet.
01:30
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.
01:49
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.
01:53
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
02:07
is going to be better.
02:10
And there is an extremely simple method. All it's used for is actually to return
02:17
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.
02:35
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
02:40
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.
02:55
We're going to say letter
03:00
equals self dot alphabet
03:04
and
03:06
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
03:24
self die
03:31
equals
03:32
sofa I plus
03:37
and then return letter.
03:39
So let's see if we've done this correctly. Uh, if we have, we have to.
03:45
All right. Everything's going crazy. You're Hang on a second.
03:50
There you go.
03:51
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.
04:01
And then we should be able to four I range
04:13
not in range for I in i d
04:15
Sorry. Trip myself off their print. I d.
04:19
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
04:28
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.
04:44
So, really, the core definition is an it aerator is any class which has those two methods theater in the next.
04:49
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.
04:59
So we can see here print i d four i an I d. We're going to print I
05:03
and we can see if this actually ran. If this worked out correctly
05:06
and we'll say
05:08
Ahmad
05:13
and let's see if we got a ride on the first try.
05:15
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.
05:26
Now,
05:27
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.
05:33
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
05:51
letters this for Luke will continue filling out the eye variable.
05:57
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,
06:06
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,
06:21
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
06:40
that is, stop it oration
06:42
and all we have to do for that is, we can say,
06:46
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
06:58
braise stop
07:00
Federation.
07:02
Let's see if this works
07:08
good.
07:10
Sure enough, it doesn't because I forgot to use self on that I, which I keep doing. Let's try this again
07:18
and it hurt.
07:24
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
07:33
if self that is greater than 25.
07:36
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
07:46
and see if this one works,
07:56
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.
08:03
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
08:22
once we get to our exceptions video a little bit better. Why that doesn't create any error messages and why everything works
08:28
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.
08:41
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,
08:50
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
09:07
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
09:13
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.
09:22
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.
09:37
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
09:43
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
Similar Content