Python Create Iterator
Create an Iterator
To create an object/class as an iterator you have to implement the methods
__next__() to your object.
As you have learned in the Python
Classes/Objects chapter, all classes have a function called
__init__(), which allows you do some
initializing when the object is being created.
__iter__() method acts similar, you can
do operations (initializing etc.), but must always return the iterator object
__next__() method also allows you to do
operations, and must return the next item in the sequence.
Create an iterator that returns numbers, starting with 1, and each sequence will increase by one (returning 1,2,3,4,5 etc.):
self.a = 1
x = self.a
self.a += 1
myclass = MyNumbers()
myiter = iter(myclass)