Generator functions subclass generator?

Terry Reedy tjreedy at udel.edu
Thu Jun 18 17:51:01 EDT 2009


An iterator class is a class with an __iter__ method that returns self 
and a __next__ method that on each call returns one not-yet-returned 
item of the actual or virtual collection it represents and raises 
StopIteration when there are no more items to return.  'Generator' (3.1 
name) is one of several built-in iterator classes.

A generator function is a function with keyword 'yield' in its body. Its 
presence results in four special behaviors.
1. When the generator function is called, it returns a generator 
instance inst instead of executing its body code.
2. When inst.__next__ is called, the body *is* executed.
3. When any yield is reached, an object is returned (yielded), but 
execution of the body code is only suspended, not terminated.
4. The next call resume execution at the point of suspension.

I believe any non-recursive generator functions can be written as an 
actual iterator class, though perhaps with more difficulty. (I suspect 
the same is true of recursive generators but I have never tried one.) 
The greater ease of writing gfs is one reason we have them.

I find the first two bits of magic easier with the following simile:
a generator function is like a virtual subclass of generator.

In other words,

def gf(params):
   body

acts somewhat like the following hypothetical code

class gf(generator):
   # inherited __new__ method stores the args
   # __iter__ is also inherited
   def __next__():
     params = <stored args> # added
     body

'yield' in the body of __next__ would still have special effects 3 and 4.

I am planning to use this in my book.  Does anyone else, particularly 
Python beginners, find it helpful?

Terry Jan Reedy





More information about the Python-list mailing list