PEP 318

David Eppstein eppstein at ics.uci.edu
Mon Mar 22 11:43:08 EST 2004


In article <7xn069t5pr.fsf at ruckus.brouhaha.com>,
 Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:

> Eyal Lotem <gnupeaker at yahoo.com> writes:
> > You are not supposed to be looking for it, IMHO.
> > Generators follow the Sequence protocol and are to be
> > treated as sequences.  Thus, you should simply make it
> > clear in the
> > function-name/conventions/interface-documentation that
> > the function returns a sequence.  Whether that
> > sequence is implemented via a generator or not is an
> > implementation detail.
> 
> But that's bogus.  Python is dynamically typed which means that a
> normal function can return whatever it wants, sequence or
> non-sequence.  A generator function can't return anything, it can only
> yield.

I agree with Eyal.  A generator is a callable that, when called, returns 
an iterator of the items it generates.  The fact that it's defined using 
yield syntax instead of using return syntax is irrelevant to the caller.

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science



More information about the Python-list mailing list