gpt4 book ai didi

python exec 在 2.7 和 3.3 之间表现不同

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:36 25 4
gpt4 key购买 nike

下面的代码片段从 Python 2.7 和 3.3 获得了不同的输出。

data = {'_out':[1,2,3,3,4]}
codes = ['_tmp=[]',
'[_tmp.append(x) for x in _out if x not in _tmp]',
'print(_tmp)']
for c in codes:
exec(c,{},data)

Python 2.7 的输出:

[1,2,3,4]

Python 3.3 的输出:

Traceback (most recent call last):
File "test.py", line 8, in <module>
exec(c,{},data)
File "<string>", line 1, in <module>
File "<string>", line 1, in <listcomp>
NameError: global name '_tmp' is not defined

为了修复 Python 3.3 中的错误,我简单地将全局变量设置为与局部变量相同,即 exec(c,data,data)。知道为什么 Python 3.3 的行为与 2.7 不同吗?

最佳答案

这似乎是已知的和期望的行为,请参阅问题 13557 https://bugs.python.org/issue13557

进一步

https://docs.python.org/3/reference/executionmodel.html#interaction-with-dynamic-features

The eval() and exec() functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace.

您可以通过不对局部变量方法调用使用列表理解,或者通过全局范围提供变量来解决上述问题

改为循环

data = {'_out':[1,2,3,3,4]}
codes = ['_tmp=[]', """
for x in _out:
if x not in _tmp:
_tmp.append(x)
""",
'print(_tmp)']

for c in codes:
exec(c, {}, data)

全局环境

data = {'_out':[1,2,3,3,4]}
codes = ['_tmp=[]',
'[_tmp.append(x) for x in _out if x not in _tmp]',
'print(_tmp)']
for c in codes:
exec(c, data)

关于python exec 在 2.7 和 3.3 之间表现不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34622902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com