I study Alex Marteli Python in a nutshell , and the book assumes that any object that has a next() method, (or at least can be used as) an iterator . He also suggests that most iterators are built by implicit or explicit method calls called iter .
After reading this in the book, I felt a desire to try. I ran the python 2.7.3 interpreter and did the following:
>>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> for number in range(0, 10): ... print x.next()
However, the result was as follows:
Traceback (most recent call last): File "<stdin>", line 2, in <module> AttributeError: 'list' object has no attribute 'next'
In confusion, I tried to study the structure of object x via dir(x) , and I noticed that it has an object of function __iter__ . Therefore, I realized that it can be used as an iterator if it supports this type of interface.
So, when I tried again, this time a little differently, trying to do this:
>>> _temp_iter = next(x)
I got this error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: list object is not an iterator
But as a NOT list, it can be an iterator, since it seems to support this interface and can definitely be used as one in the following context:
>>> for number in x: ... print x
Can someone help me clarify this in my mind?