Message248079
OK, it looks like what the documentation of exec is missing is the fact that calling exec with no arguments in a non-global is equivalent to calling it with *two* arguments. That is, your "exec(script)" statement is equivalent to "exec(script, globals(), locals())". This is implicit but very much *not* explicit in the current documentation, and should be made explicit.
To be sure I'm explaining this fully: the documentation of exec says "If exec gets two separate objects as globals and locals, the code will be executed as if it were embedded in a class definition".
>>> class Foo:
... a = 10
... [a for x in range(5)]
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in Foo
File "<stdin>", line 3, in <listcomp>
NameError: name 'a' is not defined |
|
Date |
User |
Action |
Args |
2015-08-05 21:06:35 | r.david.murray | set | recipients:
+ r.david.murray, docs@python, Peter Eastman |
2015-08-05 21:06:35 | r.david.murray | set | messageid: <1438808795.56.0.415703460865.issue24800@psf.upfronthosting.co.za> |
2015-08-05 21:06:35 | r.david.murray | link | issue24800 messages |
2015-08-05 21:06:34 | r.david.murray | create | |
|