[issue13557] exec of list comprehension fails on NameError

Amaury Forgeot d'Arc report at bugs.python.org
Fri Dec 9 15:11:01 CET 2011


Amaury Forgeot d'Arc <amauryfa at gmail.com> added the comment:

This is expected and documented: http://docs.python.org/py3k/reference/executionmodel.html#interaction-with-dynamic-features
"Free variables are not resolved in the nearest enclosing namespace, but in the global namespace.", a free variable being a variable "used in a code block but not defined there".
And yes, a list comprehension defines a code block.

Try using exec(code, locals()). It's a good habit anyway to always pass a namespace to exec().

----------
nosy: +amaury.forgeotdarc
resolution:  -> invalid
status: open -> pending

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13557>
_______________________________________


More information about the Python-bugs-list mailing list