Python | Iterator | (__iter__ and __next__)
Iterators are objects that can be iterated upon. The iterator protocol for Python declares that we must make use of two functions to build an iterator- iter() and next().
- iter()- To create an iterator
- next()- To iterate to the next element
>>> a=iter([2,7,5,8])
>>> next(a)
# 2
>>> next(a)
# 7
>>> next(a)
# 5
>>> next(a)
# 8
>>> next(a)
# Traceback (most recent call last):
# File “<pyshell#31>”, line 1, in <module>
# next(a)
# StopIteration