Message 348274 - Python tracker

Message348274

Author rhettinger
Recipients Grzegorz Krasoń, rhettinger, steven.daprano
Date 2019-07-22.00:25:59
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1563755159.49.0.903780227831.issue37646@roundup.psfhosted.org>
In-reply-to
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.
History
Date User Action Args
2019-07-22 00:25:59rhettingersetrecipients: + rhettinger, steven.daprano, Grzegorz Krasoń
2019-07-22 00:25:59rhettingersetmessageid: <1563755159.49.0.903780227831.issue37646@roundup.psfhosted.org>
2019-07-22 00:25:59rhettingerlinkissue37646 messages
2019-07-22 00:25:59rhettingercreate