I am studying Alex Marteli\'s Python in a Nutshell and the book suggests that any object that has a next()
method is (or at least can be used as) an ite
Just in case you are confused about what the difference between iterables and iterators is. An iterator is an object representing a stream of data. It implements the iterator protocol:
__iter__
methodnext
methodRepeated calls to the iterator’s next() method return successive items in the stream. When no more data is available the iterator object is exhausted and any further calls to its next() method just raise StopIteration again.
On the other side iterable objects implement the __iter__
method that when called returns an iterator, which allows for multiple passes over their data. Iterable objects are reusable, once exhausted they can be iterated over again. They can be converted to iterators using the iter
function.
So if you have a list (iterable) you can do:
>>> l = [1,2,3,4]
>>> for i in l:
... print i,
1 2 3 4
>>> for i in l:
... print i,
1 2 3 4
If you convert your list into an iterator:
>>> il = l.__iter__() # equivalent to iter(l)
>>> for i in il:
... print i,
1 2 3 4
>>> for i in il:
... print i,
>>>
You need to convert list to an iterator first using iter()
:
In [7]: x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [8]: it=iter(x)
In [9]: for i in range(10):
it.next()
....:
....:
Out[10]: 0
Out[10]: 1
Out[10]: 2
Out[10]: 3
Out[10]: 4
Out[10]: 5
Out[10]: 6
Out[10]: 7
Out[10]: 8
Out[10]: 9
In [12]: 'next' in dir(it)
Out[12]: True
In [13]: 'next' in dir(x)
Out[13]: False
checking whether an object is iterator or not:
In [17]: isinstance(x,collections.Iterator)
Out[17]: False
In [18]: isinstance(x,collections.Iterable)
Out[18]: True
In [19]: isinstance(it,collections.Iterable)
Out[19]: True
In [20]: isinstance(it,collections.Iterator)
Out[20]: True
They are iterable, but they are not iterators. They can be passed to iter()
to get an iterator for them either implicitly (e.g. via for
) or explicitly, but they are not iterators in and of themselves.
List is not iterator but list contains an iterator object __iter__
so when you try to use for loop on any list, for loop calls __iter__
method and gets the iterator object and then it uses next() method of list.
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
it = x.__iter__()
Now it
contains iterator object of x
which you can use as it.next()
until StopIteration exception is thrown