Facing problem in iteration protocol of python

The link to code is -

In this code, iter function is returning self . Please explain what is the purpose of this iter function and what does return self signifies. Also, in the next function, there is no while or for loop, so when we update self.i , why does if condition is checked again and again.

hey @abhaygarg2001,
Here iter function returns self because we need to convert our iterator object ( our class ) into an iterable so that we can perform a __next__ function over it.

Any kind of a while or for loop would have been used if you wanted to return a full sequence of numbers on a single function call. Whereas with __next__ function it is called repeated times until the condition becomes false and it calls StopIteration Exception.

I hope this would resolve your doubt.

Happy learning :slight_smile: .