quick question about generator.next()

Aahz aahz at pythoncraft.com
Wed Sep 4 23:44:55 CEST 2002


In article <mailman.1031153647.10654.python-list at python.org>,
Erik Price  <erikprice at mac.com> wrote:
>
>If I have a generator object, the first value produced by the generator 
>object does not actually get produced until the first time I call 
>generator_object.next() , correct?
>
>In other words, simply instantiating the generator object doesn't 
>"activate" the generator function, rather it just creates it -- only 
>when the next() method is called does the generator's internal logic 
>"activate", right?

Correct.  I find that although technically inaccurate, it's a lot easier
to think of generators as functions that return iterator objects.

>(Where are generator objects getting their next() method from anyway... 
>is it implicitly inherited behind the scenes when I use the "yield" 
>keyword to turn a function into a generator?)

Generator objects conform to the iterator protocol.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Project Vote Smart: http://www.vote-smart.org/



More information about the Python-list mailing list