Message 348274 - Python tracker

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Content
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.