__next__()
and __iter__()
methods, in the following way:
In [ ]:
class yrange():
def __init__(self, max_i):
self.i = 0
self.max_i = max_i
def __iter__(self):
return self # Compulsory when implementing the iterator protocol
def __next__(self):
if self.i > self.max_i:
raise StopIteration # Compulsory (for finite iterators) when implementing the iterator protocol
else:
self.i += 1
return self.i - 1
In [ ]:
iterator = yrange(5)
next(iterator)
In [ ]:
next(iterator)
In [ ]:
iterator.__next__() # This is also possible
In [ ]:
next(iterator)
In [ ]:
next(iterator)
In [ ]:
next(iterator)
Iterators are commonly used in for
structures:
In [ ]:
for i in yrange(10):
print(i, end=' ')
iter()
:
In [ ]:
i = iter(['a', 'b', 'c']) # A list
i.__next__()
In [ ]:
i.__next__()
In [ ]:
i.__next__()
In [ ]:
i.__next__()
Most of the native containers in Python can be populated with iterators:
In [ ]:
list(range(10))
In [ ]:
tuple(range(3,9))
Strings also support the iterator protocol:
In [ ]:
set('hola')
In [1]:
list_of_squares = [x*x for x in range(3)]
In [2]:
list_of_squares
Out[2]: