We are going to discuss Spight on iterators.
So it triggers a prison in Biton and they are mostly implemented within four loops.
Comprehensions, generators, etc., but hidden in plain sight.
So a delegate in Bryton is simply an object that can iterated upon an object which will return data
out of one element at a time.
So this iterator will be operating on a sequence of elements.
And each and every direction it does pick up one element at a time.
So it every day in Python is simply an object that can be iterated upon a an object which is which will
return data.
One element at a time.
Technically speaking, by Con Ed Object must implement two special methods.
So what are the two spatial methods.
That is underscored, underscored etre underscore underscore method and underscored underscore next
underscore underscore method.
So it's the part.
And the next method collectively called the iterator protocol.
So determine what that is.
It will retrieve the respective value at the next table.
Check whether the next element is there in the sequence or not.
So an object is called iterable, if we can get an interpreter from it.
And most of the built in containers in Biton, like the least double strings, etc., are incredible
because we can extract we can get iterator from it that it did function, which in turn called that
ether that is underscored, underscored, etre, underscored, underscore method, and he turns an interpreter
from them.
That next that is underscored, underscore, next, underscore, underscore.
My method must return.
The next item in the sequence on the reaching at the end.
And in the subsequent calls it must raise stop intention.
So what about where having no item in the eido in the iterator remaining.
But you are calling this underscore.
Don does go next underscore to 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 Biton visitors can easily be
implemented in Biton coding.
So here is the demonstration for you.
Iterators Ahmedi, come on in about Biton programming language.
And these are elegantly implemented within our four lubes generators.
Comprehensions except dropped but hidden in plain sight and it deleted in Biton 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 only it will return the data otherways.
It will throw some exception.
So technically speaking, Biton Iterator Object must implement two spatial methods.
One is the next method.
So Cannell's we call does underscore the underscore next underscore Londis committed and are the ones
that etre method that is actually underscored, underscore, etre, underscore, underscore method.
And collectively they will be known as the iterators protocol.
An object is called iterable if we can get an interpreter from it.
Most of the building contained at enormous Biton like above string like of a couple.
At least we are happy.
They are nothing but eatables that told me taught or the ITR function which in in tone called the it
dirt underscored underscore etre underscored underscore method returns an 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 etomidate thought from using this ITAM method
from this.
It from this list but getting one iterated and to access that data from this iterator.
We are using the method next here.
So either you can use this next.
Already did.
So this is a syntax to retrieve elements from this reiterated.
That is, I want you to others we can write already to 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 part 477.
Eleven and 33.
So for the fast next, I'm doing 44.
Next, I'm retreating.
Ahead is 77.
Next element, which will be script.
Here is eleven.
Next element, which will be Tupa here is 33.
So now we have we have got all the four elements.
Now, now, if we go for next our editor, it will raise one exception.
So let let us go for the execution of this block.
So here we are having 44.
77 IL event and 33 because the elements were 44.
77.
Eleven and 33.
So when we executed print next to our editor, then 44 was retrieved.
Our next print.
Next, our editor when we execute this method.
Then 77 has been retrieved from this iterator.
It is already dead.
Next time event.
Next time.
33.
Now, if I execute this one, I shall not put this one in comment.
If I execute this one, then one exception will be there.
And that is a stop iteration.
And this will raise error.
No items left.
As you 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 have 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 doch string.
So it should be enclosed either within a triple single codes 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 as Izabel digital is the default argument.
So whatever the value will be passing that will be assigned to this max instance available.
So here we are just defining this underscore, underscore, itr, underscore, underscore method.
And similarly, this underscore, underscore, next underscore, underscore method.
So these two methods we're defining here.
So sell dark in is digital also Ania's.
And at that instance variable is getting initialized with the Vallecito.
And then we're having this next.
So if cell gotten in is less than equal to sell dark max then there's Ziegel two to start start.
That is two to the ball of self-doubt.
And so up in will be assigned to the result.
And so dot and will be increased by one.
And the result will be returned.
Otherwise that else in the ls part we are going for stop iteration where it is in this error.
That is a stop iteration.
So now here at first we are going to print this doch string.
There is a documentation string enclosed within Tribbles.
Single course can also be enclosed within, say, triple double ghats.
So Heinberg we are printing this.
This one there is a pile of two.
That is a class named Dart.
And underscore, underscore, dog.
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 a director 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 Ziegel two powerful force.
So now this fall will be assigned to Max.
This fall will be assigned to Max or Max has got a value for here.
So now we are going for this.
Am I physical to eat at a.
So we are going for this.
Next day.
It is going on.
Going on printing.
That is to do Prop zero because Ania's having the initial value zero here.
So to really prop zero the result will become one.
And return results or that result will be printed as one.
So next time this aid has got to increase by one or two to put up one.
The result will have the value to return result.
That means it will return the value to here.
So two is getting printed because we are called this next day from this print method.
So now next time the value of in will become two.
So to do put up to become four.
So Rizzle will have the value fall.
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.
That is a constructor, this ITR method.
The next method and the respective object were defined under the class.
That is about a.
So this Max has got initialized with the value for which has been passed as the parameter to this constructor.
Then out of this eight we're creating one ITR.
So these are iterator iterator is this one I and from Iterator we can pick up the next fellows.
So there is a next of I.
So this method is getting called.
So we can also call it as I dot underscore, underscore, next underscore, underscore metatarsal.
Let me bring it here.
So I dot underscored on Dusko next underscore underscore Matassa ENDESA also we can print it.
So now let me, let me go for the execution of the same.
So I'm just it is in this part it is not required.
OK, so now if we execute the same I'm getting the same award 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 dock string.
The name of the class is in fine at it.
And this here we are having this method ito to define the next we have defined.
So this is the infinite itr object.
The class object is I from design.
We're creating one iterator using the method etre.
And then we're going for this next E.
So here you see the Nahm has got the value one.
So narm is the goal to sell dot num.
So num has got the L1 here.
So dark name is plus 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 does incrementing this value up.
Number two.
Each and every time.
And then returning that number.
There is a previous value of none will be returned.
So now we're having this next day, the next day, next in this way, if we go on all in the next method.
So at first, the NUM was having the value one.
So this one will be assigned to NARM, which will be returned.
So that is getting printed.
So one week had been dead, then three, then five, then seven.
In this way the values will go on printing.
So it has got no dominating logic.
So that's why it is infinite.
Peter, 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 about
Python programming.
Thanks for watching this video.