R. David Murray added the comment:

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

----------
assignee:  -> docs@python
components: +Documentation
nosy: +docs@python
stage: resolved -> needs patch
title: Incorrect handling of local variables in comprehensions with exec() -> 
exec docs should note that the no argument form in a local scope is really the 
two argument form

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue24800>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to