PEP 255: Simple Generators

David Bolen db3l at fitlinxx.com
Wed Jun 20 16:03:47 EDT 2001


"Russell E. Owen" <owen at astrono.junkwashington.emu> writes:

(...)
> Since a generator is very different than a function, why not mark it as 
> such right up front?
(...)
> I agree that revealing the internal implementation is bad. I disagree 
> that return is reasonable, though it is much less of a concern if a 
> generator doesn't look like a function right up front.

But isn't a generator really just a function that is returning an
iterator?  That is, why is it "very" different from a function,
internal implementation aside, which we agree it's not necessary to
reveal.

If I have a function that returns a sequence, then the caller is free
to iterate over that sequence.  A generator returns an iterator for
which the caller is equally able to do so.  Or in either case to apply
any other methods that are applicable to the returned object type.

They're a lot more alike than not to my mind - the yield permits the
generator itself to be written more elegantly, but to the caller its
still largely a function that returns an object supporting a
particular interface.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list