What makes an iterator an iterator?
Steven D'Aprano
steve at REMOVE.THIS.cybersource.com.au
Thu Apr 19 03:14:38 EDT 2007
On Wed, 18 Apr 2007 19:45:50 -0700, Alex Martelli wrote:
> 7stud <bbxx789_05ss at yahoo.com> wrote:
> ...
>> Can you explain some of the details of why this code fails:
> ...
>> def next(self):
>> for word in "Norwegian Blue's have beautiful
>> plumage!".split():
>> yield word
>
> Sure, easily: a loop like "for x in y:" binds an unnamed temporary
> variable (say _t) to iter(y) and then repeatedly calls _t.next() [or to
> be pedantic type(_t).next(t)] until that raises StopIteration.
>
> Calling a generator, such as this next method, returns an iterator
> object; calling it repeatedly returns many such iterator objects, and
> never raises StopIteration, thus obviously producing an unending loop.
Thank you for that answer Alex, even though I didn't ask the question I
was wondering the same thing myself.
--
Steven.
More information about the Python-list
mailing list