I noticed some odd behavior relating to eval(). First, a baseline case for behavior:
>>> def test(): ... x = 5 ... return [a for a in range(10) if a == x] ... >>> test() [5] So far so good. Now let's try eval: >>> c = compile('[a for a in range(10) if a == x]', '', 'single') >>> eval(c, globals(), {'x': 5}) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "", line 1, in <module> File "", line 1, in <listcomp> NameError: global name 'x' is not defined Looks like 'x' is searched for only among the globals, bypassing the locals. The same behavior is seen between exec and eval, with and without compile, and between 3.1.3 and 3.2rc2. Given simpler code without a list comp, the locals are used just fine: >>> c = compile('a=5; a==x', '', 'single') >>> eval(c, {}, {'x': 5}) True Could anyone help me understand these scoping rules? Thanks.
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor