Generator functions subclass generator?

Discussion in 'Python' started by Terry Reedy, Jun 18, 2009.

  1. Terry Reedy

    Terry Reedy Guest

    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
    Terry Reedy, Jun 18, 2009
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    428
    jstorta
    Feb 20, 2006
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,576
    Victor Bazarov
    Jun 21, 2004
  3. S.Volkov
    Replies:
    2
    Views:
    205
    S.Volkov
    Mar 12, 2006
  4. Trans
    Replies:
    8
    Views:
    306
    Robert Klemme
    Oct 23, 2008
  5. Fab

    Subclass of subclass

    Fab, Aug 9, 2012, in forum: C++
    Replies:
    0
    Views:
    383
Loading...

Share This Page