Calling a generator multiple times

James_Althoff at James_Althoff at
Fri Dec 7 15:03:01 EST 2001

Courageous wrote:
>Thefact remains, as you state that you understand below,
>the current implementation of generators is
>counter-intuitive, misleading, and incongruous with
>Python itself.

As someone who has the vast experience of writing *one* generator in Python
<wink> -- in the Interval/IteratorBounds exercise that I recently posted --
I, on the contrary, found the generator to be not-counter-intuitive,
not-misleading, and not-incongruous-with-Python.  I guess it depends on
your point of view.  For my exercise, the generator made it easier and more
straightforward for me to create the iterator that I wanted compared with
the alternative of creating an iterator class.

So as to not confuse myself, I think of generators thusly:

A generator is a function or method that -- by virtue of using the yield
statement -- *creates* and *returns* an iterator object when invoked by a
caller.  The caller normally takes that returned iterator object and uses
it by invoking its "next" method until StopIteration is raised.  The code
inside the generator function or method *runs* when the "next" method of
said iterator is invoked.  Each time a caller invokes the *generator*
function or method, a new iterator object is created and returned.


More information about the Python-list mailing list