PEP 255: Simple Generators

Glyph Lefkowitz glyph at twistedmatrix.com
Sun Jun 24 16:18:32 EDT 2001


On Thu, 21 Jun 2001, Neil Schemenauer wrote:

> PEP 255 is not intended to solve everyone's problems.  It is an
> attempt at doing _something_ useful.  Is it better to have
> generators in Python 2.2 or should we wait indefinately for
> microthreads?  Even if we do get microthreads I still see the
> extra syntax being useful.

If python's answer to "what should we do with new feature X"? is "add a
keyword" it will turn into Perl before too long.  I think that most of us
agree that print>> is a disturbing symptom of this already :)

And yes, we should wait indefinitely for microthreads.  If generators are
implemented as a syntax feature, then microthreads are implemented as a
module (or should we have syntax for that too?  'task' anyone?) then we're
again falling prey to a sin of perl; there will be more than one way to do
it.

This is, after all, not a new *capability* of python per se, like the
ability to parse XML or display 3D objects using OpenGL; it's just an
attempt to build a particular idiom into the language, by providing a
convenience.  Of course, it's all a matter of opinion (and the BDFL's
opinion is the only one that really matters), but I think that sometimes
this works -- 'for', for example -- but 'yield', and generators in
general, are too specific of an idiom to codify at the syntax level.  It
reeks of perl's arbitrary syntactic nastiness... and at least perl has a
way of *dealing* with syntax nastiness!  Without some keyword distinction,
you're going to instantly break anyone who's calculating yield, for
example. (Anyone using python for investment calculations?)

At least from my point of view, generators are a solution to a problem
which does not exist.  There's nothing I can see which would be
*significantly* easier using generators.

In addition, the implementation side of this is just an efficiency hack.
It's not hard to make this work using threads; but since python doesn't
have real microthreads, generators won't be built on top of anything
except the interpreter itself; and they will hinder the future
implementation of microthreads should they happen and probably cause some
pretty fruity interoperability issues (if it's a syntax feature).

> Have you ever tried implementing generators using Stackless's
> continuation primitives?  How would you explain to new users what
> those magic functions do?

New users should not be using generators -- it's an advanced control
structure with some interesting uses, but neither would I try to explain
to a newbie how to write a task scheduler by putting hooks into ceval.c.  
Programming is just hard sometimes.  (This is one of the reasons I use
python and *NOT* icon; for me, generators are not an important enough
feature to worm their way into everything.)

                      ______      __   __  _____  _     _
                     |  ____ |      \_/   |_____] |_____|
                     |_____| |_____  |    |       |     |
                     @ t w i s t e d m a t r i x  . c o m
                     http://twistedmatrix.com/users/glyph






More information about the Python-list mailing list