Why doesn't eval of generator expression work with locals?
gagsl-py2 at yahoo.com.ar
Thu Jan 29 22:52:55 CET 2009
En Wed, 28 Jan 2009 18:06:01 -0200, Peter Otten <__peter__ at web.de>
> Gabriel Genellina wrote:
>> (I think this has to do with free variables in the "right side" (after
>> the "in" keyword) of a generator expression; they appear to be evaluated
>> when the expression is *defined*, not when is is *used*. By contrast,
>> variables in the "left side" appear to be evaluated when the expression
> Indeed, the right side is evaluated in the enclosing namespace and then
> passed as an argument to the genexpr:
> >>> dis.dis(compile("(x for y in z)", "nofile", "exec"))
> 1 0 LOAD_CONST 0 (<code object <genexpr> at
> 0x2b22b2dcf828, file "nofile", line 1>)
> 3 MAKE_FUNCTION 0
> 6 LOAD_NAME 0 (z)
> 9 GET_ITER
> 10 CALL_FUNCTION 1
> 13 POP_TOP
> 14 LOAD_CONST 1 (None)
> 17 RETURN_VALUE
Yes, this is explained (in not so much detail) in the Language Reference.
Now I *know* how it works, but still isn't *obvious* to me.
More information about the Python-list