[Python-Dev] PEP 289: Generator Expressions (second draft)

Greg Ewing greg at cosc.canterbury.ac.nz
Thu Oct 23 23:19:56 EDT 2003


> I've checked in an update to Raymond's PEP 289 which (I hope)
> clarifies a lot of things, and settles the capturing of free
> variables.

I had another early-morning idea about how to deal with the free
variable issue, which could also be used when you have another form of
closure (lambda, def) and you want to capture some of its free
variables.

Suppose there were a special form of assignment

   new x = expr

If x is not used in any nested scope, this is the same as a regular
assignment. But if it is, and consequently x is kept in a cell,
instead of replacing the contents of the cell, this creates a *new*
cell which replaces the previous one in the current scope. But any
previously create closure will still be holding on to the old cell
with its old value. If you do this in a loop, you will end up with a
series of incarnations of the variable, each of which lives in its own
little scope.

Using this, Tim's pipeline example would become

     pipe = source
     for new p in predicates:
        new pipe = e for e in pipe if p(e)

For generator expressions, Tim's idea of just always capturing the
free variables is probably better, since it doesn't require
recognising a subtle problem and then applying a furtherly-subtle
solution. But it seemed like a stunningly brilliant idea at 3:27am
this morning, so I thought I'd share it with you. :-)

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+



More information about the Python-Dev mailing list