"""
Iterators in python
"""


# Many kinds of python objects can be iterated over using the for loop:

# lists
for element in [1, 2, 3]:
    print element
# strings
for char in "123":
    print char
# files
for line in open("myfile.txt"):
    print line

# Behind the scenes, the for statement calls iter() on the object.
# this returns an iterator object - an object that has a next() method
# which returns the next element in the sequence of elements.
# When there are no more elements, next() raises a StopIteration exception
# which tells the for loop to terminate.

s = 'abc'
it = iter(s)
it.next()
it.next()
it.next()
# the next one will raise an exception:
it.next()