Illustrating one of the chapter's insets, the following example shows how a loop uses the next() method and finishes by exception, which occurs when the generator is exhuasted.
g = (c for c in "fast") |
length = 0 |
while True: |
try: |
letter = g.next() |
length += 1 |
except StopIteration: |
break |
print "Length is", length |