question about generators

Aahz aahz at pythoncraft.com
Thu Aug 15 15:48:56 EDT 2002


In article <yu993ctg3ybo.fsf at europa.research.att.com>,
Andrew Koenig  <ark at research.att.com> wrote:
>
>Steve> But don't you think this is because of the fundamentally
>Steve> different nature of "print" and "yield"?
>
>At one level, of course I do -- but on another level, I don't.
>
>A generator is a program that yields a sequence of values to its
>caller.  If I have a program that prints a sequence of values -- that
>is, a program that yields a sequence of values to the printer -- one
>might think that by changing all of the print statements to yield
>statements (ignoring formatting issues, of course), one could change
>one program to another.

Nope.  A generator is a function that creates an iterator.  An iterator
object produces values when its next() method is called.  Don't get
confused by the fact that Python generators use the same terminology as
similar constructs in other languages.  Especially don't confuse the
issue further by re-using Python terminology in idiosyncratic ways; in
Python, "sequences" refer strictly to array-like objects (lists, tuples,
and strings).  "Stream" seems to be gaining some popularity to refer to
the output of an iterator.

I repeat my suggestion that you construct a solution using "return"
instead of "print".
-- 
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