Generators and Iterators

A generator function "yields" its values rather than returning them – and one generator function can yield zero or more results:

def alphabet (): # This is a generator.
   for c in "abcdefghijklmnopqrstuvwxyz":
      yield c
for a in alphabet (): # This is an iterator.
   print a,
# result:
# a b c d e f g h i j k l m n o p q r s t u v w x y z
New Models for Processing Text and XML with Python Sam Wilmott
Slide 8 of 51        www.wilmott.ca