[issue38937] NameError in list comprehension within .pth file

Chris Billington report at bugs.python.org
Fri Nov 29 10:36:37 EST 2019


Chris Billington <chrisjbillington at gmail.com> added the comment:

Sorry for the spamming, realised I misunderstood further.

The original behaviour isn't because the exec'd code can't create new local variables - it can - it's because of the documented behaviour of exec when it gets different dicts for globals and locals:

"If exec gets two separate objects as globals and locals, the code will be executed as if it were embedded in a class definition"

So the new scope made by the list comprehension can't access the enclosing scope in which the new variable was defined, because that's how scoping works in class definitions.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38937>
_______________________________________


More information about the Python-bugs-list mailing list