[issue37646] eval() in a list comprehension
Raymond Hettinger
report at bugs.python.org
Sun Jul 21 20:25:59 EDT 2019
Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:
This used to work as you expected in Python 2.
In Python 3, list comprehensions create their own inner scope just like generator expressions.
Per the eval() docs, "if both dictionaries are omitted, the expression is executed in the environment where eval() is called."
In your code example, the inner scope doesn't have a local variable "x", so the global variable "x" is retrieved.
That said, I would have expected the inner "x" to be found as a non-local. So yes, this does seem odd an it isn't really true that "the expression is executed in the environment where eval() is called." Instead, it uses the globals() and locals() of the environment where it is called but not the nested scope. Perhaps this should be clarified in the docs if it is in fact the intended behavior.
----------
nosy: +rhettinger
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37646>
_______________________________________
More information about the Python-bugs-list
mailing list