首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python 2 compiler.ast替换Python 3,函数丢弃和常量

Python 2 compiler.ast替换Python 3,函数丢弃和常量
EN

Stack Overflow用户
提问于 2017-07-19 02:56:51
回答 1查看 488关注 0票数 0

我正在尝试将一个较旧的库转换为Python3,该库映射了名为snakefood的依赖项。我有两个部分无法确定要替换什么,因为我找不到关于这两个函数的功能的文档- from compiler.ast import Discard, Const在Python3的ast库中似乎找不到对应的库。以下是Python2中的函数调用,请参阅此处在isinstance()调用中使用的两个函数:

代码语言:javascript
复制
    def default(self, node):
        pragma = None
        if self.recent:
            if isinstance(node, Discard):
                children = node.getChildren()
                if len(children) == 1 and isinstance(children[0], Const):
                    const_node = children[0]
                    pragma = const_node.value

        self.accept_imports(pragma)

很抱歉没有理解这篇文章,我刚刚了解到AST调用试图使用这个库。非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-19 03:04:58

Discard (我花了一段时间才弄明白它是做什么的)现在是Expr (尽管这比以前包含了更多的东西)

我从编译器/codegen.py的源代码中得到了这个提示:

代码语言:javascript
复制
    def visitDiscard(self, node):
        # XXX Discard means it's an expression.  Perhaps this is a bad
        # name.

Const已经被几种不同的类型所取代,这些类型代表各种常量,特别是NumStrJoinedStr和其他一些常量。所有ast类型can be found here的文档。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45174959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档