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

F

Fabio Zadrozny

Hi All,

Anyone knows why the code below gives an error?

global_vars = {}
local_vars = {'ar':["foo", "bar"], 'y':"bar"}
print eval('all((x == y for x in ar))', global_vars, local_vars)

Error:

Traceback (most recent call last):
File "C:\temp\work\test\src\a.py", line 3, in <module>
print eval('all((x == y for x in ar))', global_vars, local_vars)
File "<string>", line 1, in <module>
File "<string>", line 1, in <genexpr>
NameError: global name 'y' is not defined

Note that if a list is used instead of a generator it works...

Thanks,

Fabio
 
J

Jon Clements

Hi All,

Anyone knows why the code below gives an error?

global_vars = {}
local_vars = {'ar':["foo", "bar"], 'y':"bar"}
print eval('all((x == y for x in ar))', global_vars, local_vars)

Error:

Traceback (most recent call last):
  File "C:\temp\work\test\src\a.py", line 3, in <module>
    print eval('all((x == y for x in ar))', global_vars, local_vars)
  File "<string>", line 1, in <module>
  File "<string>", line 1, in <genexpr>
NameError: global name 'y' is not defined

Note that if a list is used instead of a generator it works...

Thanks,

Fabio

I tend to think of it as a generator produces another scope, gets
refactored into something
similar to:

def yourfunc(ar):
for x in ar:
yield x == y


Which doesn't work either, however, if you introduce a global y the
function can access it (similar if you add y to your global_vars).
It's basically one of those scope/closure gotcha's along with lambdas
(which was discussed quite heavily recently).

hth,
Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top