Generator functions subclass generator?

T

Terry Reedy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top