We are going to discuss Biton iterators.
So iterators are present in Baton and they are mostly implemented within four loop's comprehensions,
generators, etc., but hidden in plain sight.
So a delegate in Python is simply an object that can iterated upon an object which will return.
Data are the one element at the time.
So this iterator will be operating on a sequence of elements and each and every iteration it will pick
up one element at a time.
So it's written in Python is simply an object that can be iterated upon an object which is which will
return data one element at a time.
Technically speaking, Biton iterator object must implement two special methods.
So what are the two special methods that is understood, understood, etre, underscore, underscore,
method and underscore, underscore, next, underscore, underscore method.
So it's the method and the next method collectively called the iterator protocol.
So it's a method that is to retrieve their respective value.
At the next, he will check whether the next element is there in the sequence or not.
So an object is called iterable, if we can get an iterator from it, and most of the built in containers
in Biton, like the least double strings, etc, are iterable because we can extract we can get iterator
from it, that it did a function which in turn call that etre that is understood, understood, itr
underscore, underscore method returns and iterator from them.
The next that is underscore, underscore, next underscore, underscore method must return the next
item in the sequence on the reaching at the end and in the subsequent calls it must raise stop iteration.
So whenever we are having no item in the in the iterator remaining.
But you are calling this underscore, underscore next underscore the underscore method, then one one
exception will be raised and that is a stop iteration.
So let us go for one practical demonstration to show you that how this python iterators can easily be
implemented in python coding.
So here is the demonstration for you.
Iterators are very common in our python programming language and these are elegantly implemented within
our four loops, generators, comprehensions, etc. but hidden in plain sight and iterated in Python
is nothing but an object.
And this object will return detA one at a time during our iteration.
Obviously, if some data is available, then it will return the data, otherwise it will throw some
exception.
So technically speaking, Python Étretat object must implement two special methods.
One is the next method.
So Kano's we call does underscore, underscore, next underscore the underscore method.
And out of the one is the ITR method that is actually underscored, underscored, etre underscore,
underscore the method and collectively they will be known as the iterator protocol and object is called
iterable if we can get an iterator from it.
Most of the building content in our python, like our string, like our top, at least having they are
nothing but eatables that entire method or the ITR function, which in turn called the itr underscore,
underscore etre underscored underscore method returns and iterator from them.
So let us go through this example and to discuss the property of this iterator.
So here you see here to define one list here and this item start from using this method from this it
from this list we are getting one iterated and to access that data from this iterator, we are using
the method next here.
So either we can use this next already.
So do the syntax to retrieve elements from this iterator.
That is our return address.
We can write our return dot, underscore, underscore, next underscore, underscore method.
So anyone we can use that one.
So here to see the list is containing data that is forty four, seven, seven, 11 and 33.
So for the next unretiring 44 next mean there is seventy seven next element which will be retrieved
here is eleven next eleven which will be added to here 33.
So now we have got all the four elements now.
Now if we go for next hour, later it will raise one exception.
So let let us go for the execution of this block.
So here we are having forty four, seventy seven, 11 and 33 because the elements were 44, 77, 11
and 33.
So when we execute that print next hour, greater than 44 was retrieved and next print next hour after
we execute this method, then 77 has been retrieved from this iterator.
That is our return next time.
Eleven next time.
Thirty three.
Now, if I execute this one, I should not put this one in comment.
If I execute this one, then one exception will be there and there is a stop iteration and this will
raise no items left, as we mentioned in the comment.
And so next hour it will produce this error.
That is a stop iteration.
So let us go for another one.
So create a custom iterator.
So here we are defining one class that is the power of two.
And here we are having this document documentation string.
In short, we can call it as dark string.
So it should be enclosed either within a single class or triple double quotes.
So it is also known as a block comment, and it should be the first intended, intended statement in
our class.
So next, we are having this constructor that is our underscore, underscore in it, underscore, underscore.
So here you are defining this, Max, and this is a goal.
Digital is the default argument.
So whatever the value will be passing that will be assigned to this Max instance variable, so here
we are just defining this underscore, underscore, Peter, underscore, underscore method and similarly
this underscore, underscore, next underscore, underscore method.
So these two methods we are defining here, so self-taught in physical digital.
So in another instance, rideable is getting initialized with the Vallecito and then having this next.
So if self got in is less than equal to Max, then the Israelis are due to start start.
That is due to the power of self-taught ends up in will be assigned to the result and self-taught and
will be increased by one and the result will be returned.
Otherwise that else in the part we are going for stop iteration where it is this error, there is a
stop iteration.
So now here at first we are going to print this dark string.
There is a documentation string and then triple single codes can also be included within the triple
double codes.
So whenever we are printing this, this one, there is a part of two that is a class named Underscore,
underscore, underscore, underscore.
You can see that it is printing.
Let us go for this.
It is printing this respective string that is a class to implement and iterator of powers of two.
So that string whatever is discussed or defined here.
So it has got printed.
So now we are going for this.
It is equal to power of two four.
So now this fall will be assigned to Max.
This will be assigned to Max.
So Max has got the value for here.
So now we are going for this bicycle to iterate.
So we are going for this next day.
Next day.
The next day.
It is going on going on printing.
That is to the part of zero because Ania's having the initial value zero here.
So to zero the result will become one and return result.
So that result will be printed as one.
So next time this end has got increased by one or two to one, the result will have the value to and
return the result.
That means it will return the value to here.
So too is getting printed because we have called this next day from this print method.
So now next time the value of N will become too so to become four.
So this will have the value for and the result will be returned and that will get printed.
So in this way, I'm getting this one as one to four as output.
So I think you are getting this idea how this method, which is having this in it, there is a constructor,
this ITR method, the next method and the respective object.
We are defined under the class.
That is our aim.
So this Max has got initialized with the value for which has been passed as a parameter to this constructor.
Then out of this eight, we are creating one item.
So this iterator is this one I and from Iterator we can pick up the next Palouse.
So there is a next of I.
So this method is getting called.
So we can also call it, as I underscore, underscore, next underscore, underscore method.
So let me bring it here.
So I dot underscored, underscore, next underscore, underscore methods to also we can print it.
So now let me, let me go for the execution of the same.
So I'm just erasing this part.
It is not required.
OK, so now if we execute the same I'm getting the same output because next day is actually executing
IDOT underscored, underscore, next underscore, underscore method.
So next week we are going for creating a infinite custom iterator.
So in this case, this is our Doch string.
The name of the class is Infinite Etre.
And here we are having this method literally of define the next we have defined.
So this is the infinite eater object.
The class object is I from the site.
We're creating one iterator using the method etre and then we are going for this next E!
So here you see the NUM has got the value one.
So NUM is equal to num num num has got the value one here.
Sultanahmet is equal to two.
So in this way it will go on creating because there is no logic is there, to stop this one.
It will go on creating, just incrementing this value of NO2 each and every time and then returning
that number is a previous value of NUM will be returned.
So now we are having this next day, the next day, next in this way, if you go on all in the next
method.
So at first the number was having the value one.
So this one will be assigned to NUM, which will be returned.
So that is getting printed.
So one will get printed, then three, then five and seven.
In this way the values will go on printing.
So it has got no dominating logic.
So that's why it is infinite etre, the name of the class has been given accordingly.
So from this demonstration, you are getting this idea that how this iterator can play its role in our
Python programming.
Thanks for watching this video.