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

Aaron Brady castironpi at gmail.com
Sat Jan 31 06:12:24 EST 2009


On Jan 31, 1:08 am, "Hendrik van Rooyen" <m... at microcorp.co.za> wrote:
> "Gabriel Genellina" <ga..... at yaz.oo.com.ar> wrote:
snip
> >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.
snip

Did anyone observe this yet?

>>> global_vars = {}
>>> local_vars = {'ar':["foo", "bar"], 'y':"bar"}
>>> print eval('all((x == y for x in ar))', local_vars, global_vars )
False
>>> print eval('all((x == y for x in ar))', global_vars, local_vars)
#error as original



More information about the Python-list mailing list