All right.
In this lecture, I'm going to show you three advanced methods of Python generators.
Now, the first one is going to be the send method.
Then we will look at throw method and we'll look at close method.
Now, let's get started with the first method.
Now, to be able to explain it, very detailed, I'm going to go ahead and create a function over here.
In this case, it's going to be the normal function.
So let's say we want to find the numbers that is divided by 555 so you can choose any number.
So in this case, I'm just going to put the number like this.
So it's going to pass the number.
So this function will take as a parameter number.
Then I'm going to check that if this number is divided by 555 is equal to zero.
So in this case, if the remainder is zero, they are just going to return true.
So this is a simple function and otherwise we're just going to put return false so we can do it like
this, or we can just delete this statement and put return like this.
So this is going to return true if the number is divided by 555.
So we can just go ahead and check it like this is divided.
So I'm just going to pass the number itself and run it.
You will see that in this case it is returning true.
Then I'm going to go ahead and use this function inside Python generator and check for infinite division
of this number.
So what I'm going to do, I'll just go ahead and put over here, define so it's going to be infinite
division, then it's not going to take any parameter.
Then I'm going to create a variable over here which should start from zero.
So it's going to the number starts from zero.
Then we are going to write Infinite Loop over here.
So while true, I'm going to check that if this number is divided by 555, so I'm going to pass this
over here, we are going to return the yield.
So as you remember, instead of return statement, we are using yield in case of the python generator.
So that's why I'm going to put yield over here.
So this is going to be the python generator function.
But as I mentioned before, we are going to use the send method of the python generator.
So to be able to use the same method, we have to create a variable over here which will be assigned
with the yield number.
So the reason that we are doing this, when you are sending the values with the send method, the variables
will be received in the yield expression.
Then we need to assign them to a variable to be able to use them inside generator function.
Otherwise the values that is sent by the send method will be lost.
So that's why to be able to do that, I'm going to go ahead and create over here a variable which is
going to be the I that I'm going to put it like this.
So then after getting this, we can go ahead and check that.
If I is not none in this case, we are going to assign this I to the number and we are going to increase
that number.
And at the end of this loop, we are going to increase that number here.
I'm going to put if condition like this.
If I is not none, you will understand why I'm putting this.
Then we're going to assign the I to the number.
Then here I'm going to increase the number by one.
So how this is going to work?
So if we are sending value with the send method over here, in this case, this I will not be none.
So in this case, we are going to assign that to the number, the value that we are getting from the
centimeter method.
So that's why when we are calling this function, we are going to send it to the value.
So when you are writing the yield keyword over here like this, it is not just returning the value from
the function, it is also receiving the value.
So this means that by using send method, we can come here and send any given value to our generator.
So for example, let's say our generator stopped at a value of 1000 so we can interrupt it and start
our generator from 2000.
But if we don't use the send method, it will continue with this number, which is going to be 1001.
But if you want to skip some numbers and continue from the any given number in this case, you can use
the same method to be able to send the number over here.
To demonstrate this, I'm going to interact with this generator by using loop.
So we have learned that there are various ways of interact with the generator.
So in this case I'm going to use loop.
So first we need to create the generate object.
So it's going to be an object is equal to I'm going to call infinite division function.
Then here I'm going to write for I in generator object, then we can go ahead and print out the I.
So let's say, what will be the result over here.
Then I'm going to go ahead and pass some numbers to be able to start over here.
Now we can increase the digits every time.
So that's why what I'm going to do, we can take the length of this.
I so I will just put over here.
Digit is equal to length of SDR.
I then we are going to pass number two inside this yield statement.
So to be able to pass a number, we are going to use generator object to send, then I'm going to put
over here to increase the digits.
So it's going to be ten squared the number of the digits.
So I'm just going to put over here digits.
So every time it's going to start.
Our generator from this number over here.
Now, if I just go ahead and run our code, you will see that it is continuing like this.
Lots of numbers are coming to the console.
So I'm just going to stop this.
So here what we can do to be able to see the numbers in the console, how this is working.
We can put the pulse, so I'm going to import the import time.
Then we can put the 3 seconds slip over here to be able to see the results very well.
So I'm just going to put over here time dot sleep.
So I'm going to put 3 seconds then let's just clear everything from here and I'm just going to run this.
You will see that first number is zero.
Then we are going to wait for 3 seconds.
Then the second number is this one.
Then, as you can see, the number of digits is increased.
Then the third number is this one.
And it continues like so as you can see, every time the number of digits is increased.
But if you do it without send method, it's going to be something 555 is multiplied with this three
so we can check the results like this.
So here we have put the yield statement to be able to get the value with the send method.
Now if I just change it to like this, you just return the number.
And for now I'm just going to comment out this part.
Then let's just comment out this part.
Also, we are not sending anything.
Now, if I run this one, you will see that in this case it's going to be increased one by one.
So as you can see, first is zero.
Then the first number that is divided by 555 is 555.
Then if you multiply it with two, it's going to be this number.
If you multiply it with three, it's going to be this number and it continues like this.
But with the send method, we are just changing these orders.
We are skipping some numbers and we are giving the numbers to be able to start the next iteration from
the number that we want to start.
So by putting the yield statement like this, you can send any given number to be able to start the
next value of the python generator from any given number over here.
Now, the next method that I'm going to show you over here is going to be the throw method.
Now, throw method is very useful when you are using the python generator like this, because over here,
if I run this, it's going to run the infinitely.
Now, if I want to stop somehow this iteration we can put over here the if condition like this.
So I'm going to put if digits then is greater than for example let's put eight then in this case we
can just put generator object dot row, then we can call for example value error.
So we can put any message over here.
The number is long.
Now, if I go ahead and run this code, you will see that it continues to do a certain level and it
will stop there.
So as you can see, when you reach certain level, it says that the number is two.
So that's how this method works.
Now we can use the similar operation by using the close method.
Now in this case it is raising error, but the close method will exhaust our generator.
So in this case, instead of zero, we can go ahead and put over here close.
Then I'm just going to change the second to one, then run our code.
It's going to be much more faster.
Then let's see the output.
You'll see that in this case it's going to raise a different error.
So as you can see, this is the stop iteration.
So we have learned that stop iteration error raises when the old values from the python generator function
exist.
So that's how advanced methods of python generator works.
So hopefully everything is clear.