Using iterator with next
A feature of any iterator is that we could iterate over it using the next
call.
from counter import Counter
cnt = Counter()
while True:
try:
a = next(cnt)
print(a)
except Exception as ex:
print(ex.__class__.__name__)
break
Output:
1
2
3
StopIteration