Why doesn't eval of generator expression work with locals?

Hendrik van Rooyen mail at microcorp.co.za
Sat Jan 31 02:08:20 EST 2009


"Gabriel Genellina" <ga....2 at yaz.oo.com.ar> wrote:


>Consider this expression: g = (x+A for x in L for y in M). This is  
>currently expanded more or less like this:
>
>def foo(L):
>   for x in L:
>     for y in M:
>       yield x+A
>g = foo(iter(L))
>
>(as your example above) Note that L has a special status -- it's the only  
>expression evaluated at the time g is defined. It *could* have been like  
>this:
>
>def foo()
>   for x in L:
>     for y in M:
>       yield x+A
>g = foo()
>
>or even like this:
>
>def foo(L, M, A):
>   for x in L:
>     for y in M:
>       yield x+A
>g = foo(iter(L), iter(M), A)
>
>In particular, I like the 2nd (all late binding). Seems this topic was  
>discussed many times [1] when PEP289 [2] was proposed, and "practicality  
>beats purity".

Ok thanks - I see where you are coming from now.
It never ceases to amaze me how different we all are -
Given a choice, I would have chosen the last alternative,
as it feels more natural to me - the one you like feels to
me as if it were too tightly coupled, somehow. - almost
as if you were using named globals in a function instead
of passing arguments in.

But hey - I have long since learned that it is of no use to
rant against the way something works, if you cannot
easily change it - one can just figure out what it does, 
and then figure out how to use it to get it to do what one
wants.

Thanks for an interesting discussion.

- Hendrik





More information about the Python-list mailing list