[Tutor] a question about iterators
Kent Johnson
kent37 at tds.net
Tue Jun 17 14:14:35 CEST 2008
On Tue, Jun 17, 2008 at 1:46 AM, Christopher Spears
<cspears2002 at yahoo.com> wrote:
> I am confused by this statement:
>>>> i = iter(a)
>
> Why do I need to turn 'a' into an iterator? Didn't I already do this when I constructed the class?
Yes, a is already an iterator.
> As a test, I tried the following:
>
>>>> for j in range(1,5):
> ... print j, ':', a.next(j)
> ...
> 1 :
> Traceback (most recent call last):
> File "<stdin>", line 2, in ?
> File "<stdin>", line 13, in next
> StopIteration
>>>>
>
> Why didn't that work?
Did you make a new a or reuse the same one? The old a has reached the
end of its values so it raises StopIteration.
Note that passing parameters to next() is not part of the iterator protocol.
Kent
More information about the Tutor
mailing list