We have discussed that if we pass and I did a bill to the ITER function it returns a nice data object
and on this ITER data object we can call the special method double underscore next.
Similarly when you parse the item data object to the ITER function in this case as well it returns itself
or the either data object itself and on this ITER rated object we can call the method double underscored
next.
So the purpose of the ITER function or the they shouldn't matter double underscore ITC R is to return
an object on which the special method double underscored next can be called.
So let us now parse the I trouble list.
1 2 The ITER function then execute.
So this has returned the ITER function has return and list iterator object.
Now let us pass a string to the ITER function and then execute.
So this is right on a string nitrate that object.
Similarly let's parse a dictionary to the ITER function and then execute.
So this is written a dictionary key ITER later.
Now letters pass a list to the ITER function and this is assigned to the variable ITER one which makes
the variable ITER 1 a list I directed object.
So here we have the list iterator.
Now we have also discussed that when we pass an item that object to the ITER function it returns the
idea that object itself.
So let's see that.
Let's parse the object ITER one execute.
So this has returned the iterator object itself.
The ITER function in order to verify if the ITER function has return a list iterator object we can use
the double equal to operator.
So let's see.
So I have passed the ITER rated object to the ITER function and using the double equal to.
Operator I'm gonna check if this is same as the ITER object IDR 1 or not.
So let's execute and this is true in order for the class to be either rated or word.
We need to define these special methods.
I did and next and we know that the ITER method returns the ITER object itself.
So when you define the ITER method in your eye trouble class you need to have a written statement that
they don't sell returning cell from a method implies that your method returns a reference to the same
instance object on which it was called.
And if you do not want your class to be a nitrate or in that case the IDR method need not add it on
self coming to the special method next.
This contains the logic to compute the next element in the iterator object the method ITC at and next.
Collectively call the ITER data protocol in Python and I directed object implement the ITER data protocol
which consists of these two methods.
So we use the idea data protocol when we have a custom object that we want to be a durable.
We will discuss some examples to understand how custom objects can be iterated over.
So we have an input sequence a which is an eye trouble that is a list now using the ITER and the next
method.
We want to either it or what the elements in this durable.
So instead of using a for loop we are going to manually loop over the elements in the list.
So for that we have defined the class example 1.
So we have defined an init method.
No order to initialize the parameters and then we have defined the IATA and the next method.
The IGAD method has two statements.
We know that the next method needs a nitrate or object in order to iterate over the elements.
So we are going to convert the input sequence which is an either a bill in to an idea later object using
the IDR method and then we need to have a written statement which is going to return the iterated object
itself.
Now coming to the next method within the next method we are going to use a while loop and index numbers
in order to iterate over the elements in the item data object and within the by loop we have used the
try except.
Indeed try blah.
We have this statement and in case this statement raises an error that is if there are no more elements
in the item later object.
In that case we are going to catch this exception.
The exception is index error.
We are going to raise this error.
Stop iteration and if if this statement does not raise any error that guess we are going to print the
item or the element and break out of the loop.
The stop iteration error.
We are raising.
If there are no element in the item that object is used to signal the end of the item nation.
So when there are no more values left to compute the next true method is required to raise a stop iteration.
Exception.
So when we see these top iteration error we know that we have exhausted the item data.
So this is normal behavior in all ITER does.
So we have defined the class.
Now let us go and create an instance.
Let us call this TRL 1 and we are going to pass the list a to the class.
Example 1.
Then execute.
So here is the instance object.
No let us convert the instant object to an item later object by passing it to the ITER function.
So let's execute this.
So IDR 1 is an item later object.
We now have an operator object which is generated by the IDR function.
So on this I directed object we can call the next function.
So let us parse this object to the next function.
So it has generated the next function has generated the first item in the item that object.
I d e r 1.
Now if we want to compute or generate the next item in the sequence we need to call the next method
again.
So let's call the next method again on ITV 1.
And here is the next Matar and here is the next round you select.
Keep calling the next function on the idea that object 4 5.
So we have defined five elements in the list and then the next function or the next method has reached
the end of the item later object.
The next function has encountered index error and this stop operation exception has been DREs.
If you call the next function again and again on the same I directed object it is going to raise the
same exception stop ideation.
This is because there are no values or no more values to be computed in this outdated object.
So there is no use of the item data object when all of these values have been consumed in this object.
So if you want to generate the same set of numbers you can recreate the same object or you can create
a new instance object with the same input sequence.
Now let us try to generate an arithmetic sequence using a nitrate object.
So an added Matic sequence is a sequence of numbers such that the difference between the consecutive
terms is constant.
Difference here means the second term minus the first term.
So the difference between the second and the first terms is three difference between the third and the
second term is three.
The third and the second terms is three again and so on.
And this is the formula to calculate the end term in the sequence in here represents the number of the
tone in the particular sequence.
So let us use the same formula to generate this arithmetic sequence.
So this is the class arithmetic progression which has implemented the I treated protocol which means
the special method I.D. card and next method if you'll notice the 80 odd method is going to return itself
which means it is going to return the iterator object itself.
So let us go ahead and create an instance of this class arithmetic progression.
Let's call this instance a b 1 and let's parse the arguments the first term is 1.
We want to generate the first 10 items in the sequence and then the difference between the consecutive
numbers in this sequence should be three.
So let instantiate the object.
So here we have the object AP 1 let's pass it to the TR function.
Now on this item data object we are going to call the next function the next function has generated
the first term in this sequence.
So let's keep calling the next function on this.
I directed object or is the next number in the sequence 7 is the next number in the sequence 10 13 16
22 twenty eight.
And then after the fourth 10 numbers in the sequence have been generated this top iteration exception
has been raised by the next function.
Even if you call the next function again or wouldn't work the item rate or object it's going to raise
the same exception because there are no more values to be computed.