Meta: PEP discussion (was Re: PEP 255: Simple Generators)

Tim Peters tim.one at home.com
Wed Jun 27 00:07:59 EDT 2001


[Tim]
> We didn't *have* to expose Python's generator-iterator object
> either

[Greg Ewing]
> Out of curiosity, what would a Python generator implementation
> look like that didn't expose generator-iterators? What would
> happen if you called a generator outside of a for-loop
> context?

The implementation would probably be almost exactly the same, but the
*visible* behavior would probably look like either Icon or, less likely,
CLU.

In CLU it was impossible (a compile-time error) to invoke an iterator
outside of an iterative control context; that would correspond in Python
terms to raising an exception if you tried to use a generator anywhere but
after the "in" of a for/in loop (easily arranged by the PVM, if that was
desired).

The Icon version can't know at compile-time, so I bet Python would have
acted more like that:  you can invoke a generator anywhere, but when invoked
outside of a generative control context, the generator would yield its first
result, and that's all.  You couldn't resume it -- without the possibility
for a *named* resumable object, only magical control contexts can manage to
tickle the thing more than once under the covers.

IOW, without exposing the generator-iterator object, all the concern about
functions and generators being "radically different" would have been a
non-issue -- but generators would have been truly *usable* only in for-loops
(Icon has many kinds of generative control context, though, so the
limitation isn't so severe there).  The only thing the generator-iterator
object buys you is a user-invocable .next() method, but that packs a lot of
bang for the buck.  In return, it's *visible* that a generator-function
returns a generator-iterator, hence all the "radically different" stuff.






More information about the Python-list mailing list