python - 当我使用全局变量运行 eval 时,为什么不复制全局变量?

标签 python eval

我很难理解 eval() 对计算表达式中使用的全局变量的行为。

例如,下面的脚本打印1:

x = 1
print eval('x')

虽然以下脚本因 NameError: name 'x' is not defined 而失败:

x = 1
print eval('x', {})

但是,来自 the documentation of eval() for my Python version (强调我的):

The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the globals and locals dictionaries as global and local namespace. If the globals dictionary is present and lacks ‘__builtins__’, the current globals are copied into globals before expression is parsed.

因此,根据这个,由于存在 globals 参数并且确实缺少 __builtins__,我希望所有当前的全局变量 - 包括 x - 在计算表达式之前复制到其中;但显然情况并非如此。我错过了什么?

最佳答案

这似乎是一个错误。我不知道是文档还是实现中的错误,但是 eval 不会将当前全局变量复制到 globals if __builtins__不存在。相反,it only copies __builtins__ :

if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
    if (PyDict_SetItemString(globals, "__builtins__",
                             PyEval_GetBuiltins()) != 0)
        return NULL;
}

我在 Python bug tracker 上没有找到任何相关信息,并且在 3.4 和当前的开发分支中仍然存在差异,因此可能值得提交错误报告和建议的文档更正:

If the globals dictionary is present and lacks ‘__builtins__’, the current __builtins__ is copied into globals before expression is parsed.

关于python - 当我使用全局变量运行 eval 时,为什么不复制全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24934908/

相关文章:

python - Scrapy 将返回的项目存储在变量中以在主脚本中使用

javascript - 如何在 javascript 中深度嵌套的对象成员上使用 Ramda 的 omit() 函数

javascript - 用 Javascript 中的 eval 替换重复出现的循环 - 好还是坏?

asp.net - <%# Eval ("State") %> 或 <%# DataBinder.Eval(Container.DataItem, "state")%>

r - 使用 R 中的数据掩码评估最大似然表达式

python - Comint-Mode 有类似 eval-print-last-sexp 的功能吗?

Python Pandas 在函数中处理数据帧

python - 如何获取多个列表并嵌套它们?

python - 如何自动从数据库中选取一个元素?

python - 消除重复项并对列表中的相应项求和