Calling a generator multiple times

Greg Ewing greg at cosc.canterbury.ac.nz
Thu Dec 6 19:54:53 EST 2001


Courageous wrote:
> 
> The first call to some generator g() should behave like:
> 
>         f=g()
>         f.next()
> 
> Subsequent calls to g() should behave like
> 
>         f.next()

Er... where is this "f" going to be stored? Before
you answer that, make sure you've considered

   for i in g():
     for j in g():
       print i, j

> ... As long as the generator is defined with a signature
> which makes it appear as an ordinary function, that's exactly
> how it should behave.

When generators first appeared, quite a number of
people, including me, argued fervently and passionately
that generators *shouldn't* look like ordinary functions,
for this very reason. Alas, our pleas fell on deaf ears.

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list