Raymond Hettinger <raymond.hettin...@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 <rep...@bugs.python.org> <https://bugs.python.org/issue37646> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com