[Python-Dev] An issue recently brought up inpatch#872326(generator expression)

Tim Peters tim.one at comcast.net
Tue Mar 23 15:39:57 EST 2004


[Guido]
>> All this makes me lean towards getting rid of the binding capture
>> feature.  That way everybody will get bitten by the late binding fair
>> and square the first time they try it.

[Raymond]
> I prefer this approach over one that has subtleties and nuances.

I'm afraid it's not that easy.  There's no precedent in Python for delaying
evaluation of anything in an expression without an explicit lambda, so late
binding doesn't escape "subtleties and nuances" either -- to the contrary,
in some natural senses late binding is maximally surprising in a Python
expression.

http://mail.python.org/pipermail/python-dev/2003-October/039323.html

spells out the subtle bugs that follow from late binding of either "p" or
"pipe" in this example (which I still think "is-- or should be --an easy
application for pipelining generator expressions"):

    pipe = source
    for p in predicates:
        # add a filter over the current pipe, and call that the new pipe
        pipe = e for e in pipe if p(e)

I suppose the guts could be written:

  pipe = (e for e in (lambda pipe=pipe: pipe)() if (lambda p=p: p)()(e))

That's the ticket -- nothing subtle or nuanced about that <wink>.  I'm not
sure enough of the intended late-binding semantics to guess whether

  pipe = (e for e in (lambda pipe=pipe: pipe)() if (lambda p=p: p(e)))

would work the same way, but figure it would.

OTOH, I'm tempted to say I don't really care if generator expressions are
prone to disaster when they get non-trivial.  That would be a self-serving
but anti-social thing to say.




More information about the Python-Dev mailing list