Python:深度复制ast节点树
我正在尝试使用 deepcopy
(来自 copy
模块)从 ast
模块深度复制节点树。
这似乎不起作用。当我使用复制的结果时(我检查了它;它确实在复制的节点中丢失了),我遇到了奇怪的错误,例如 TypeError: required field "name" Missing from FunctionDef
,所以它没有没有正确复制它们。
有什么技巧可以让它发挥作用吗?或者也许我错过了什么?
I'm trying to use deepcopy
(from the copy
module) to deeply copy a node tree from the ast
module.
This doesn't seem to work. I'm getting strange errors like TypeError: required field "name" missing from FunctionDef
when I use the copied result (and I checked it; it really is missing in the copied node), so it didn't correctly copied them.
Is there a trick I can make this working? Or maybe am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,我错了。
copy.deepcopy
似乎工作正常。我认为它不起作用的原因是因为这种非常奇怪的行为:这在 PyPy 中返回
None
。可能是一个错误,因为在 CPython 2.6 中,我得到了foo
。奇怪的是,在 PyPy 中,如果我从ast.FunctionDef
调用中删除name=None
,我也会得到foo
作为输出。我为此创建了一个 PyPy 错误报告。
Sorry, I was wrong.
copy.deepcopy
seems to work correct. The reason I thought it wouldn't work is because of this very odd behavior:This returns
None
in PyPy. Probably a bug because in CPython 2.6, I getfoo
. Strangely, in PyPy, if I removename=None
from theast.FunctionDef
call, I also getfoo
as the output.I created a bug report for PyPy about this.