➜

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.

作者 rhettinger
收信人 Grzegorz Krasoń, rhettinger, steven.daprano
日期 2019-07-22.00:25:59
SpamBayes Score -1.0
Marked as misclassified 是
Message-id <1563755159.49.0.903780227831.issue37646@roundup.psfhosted.org>
In-reply-to
内容
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.
历史
日期 用户 动作 参数
2019-07-22 00:25:59rhettinger修改recipients: + rhettinger, steven.daprano, Grzegorz Krasoń
2019-07-22 00:25:59rhettinger修改messageid: <1563755159.49.0.903780227831.issue37646@roundup.psfhosted.org>
2019-07-22 00:25:59rhettinger链接issue37646 messages
2019-07-22 00:25:59rhettinger创建