What makes an iterator an iterator?
Alex Martelli
aleax at mac.com
Wed Apr 18 22:45:50 EDT 2007
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.
Alex
More information about the Python-list
mailing list