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

Fabio Zadrozny fabiofz at gmail.com
Tue Jan 27 18:31:58 EST 2009


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



More information about the Python-list mailing list