Why doesn't eval of generator expression work with locals?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Sat Jan 31 06:48:55 CET 2009
En Fri, 30 Jan 2009 06:42:22 -0200, Peter Otten <__peter__ at web.de>
escribió:
> Gabriel Genellina wrote:
>> g = (x+B for x in A)
>
> I think it helps understanding if you translate the above to
>
>>>> A = [1,2,3]
>>>> B = 1
>>>> def f(a):
> ... for x in a:
> ... yield x+B
> ...
>>>> g = f(A)
>>>> A = [4,5,6]
>>>> B = 10
>>>> print list(g)
> [11, 12, 13]
>
> This is not altogether unintuitive, but I think I would prefer if it
> worked
> like
>
>>>> A = [1,2,3]
>>>> B = 1
>>>> def f(a, b):
> ... for x in a:
> ... yield x+b
> ...
>>>> g = f(A, B)
>>>> A = [4,5,6]
>>>> B = 10
>>>> list(g)
> [2, 3, 4]
>
> i. e. every name were bound early. Of course this wouldn't help with
> locals() which would still be called in different scopes.
Yep -- although I would like all names were late bound instead. But as I
posted in another message, in this case "practicality beat purity" and a
special case was made for the leftmost iterable.
--
Gabriel Genellina
More information about the Python-list
mailing list