Python 2 compiler.ast 替换 Python 3,函数 Discard 和 Const
Python 2 compiler.ast replacements for Python 3, functions Discard and Const
我正在尝试将一个较旧的库转换为 Python 3. 我有 2 个部分,我不知道要用什么来替代,因为我找不到任何一个的文档功能确实 - from compiler.ast import Discard, Const
我似乎无法在 ast
库的 Python 3 中找到等效项。以下是来自 Python 2 的函数调用,请参阅此处的两个都在 isinstance()
调用中使用:
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 调用。非常感谢。
Discard
(我花了一段时间才弄清楚它的作用)现在是 Expr
(尽管这比以前包含更多内容)
我从 compiler/codegen.py:
的来源得到了这个提示
def visitDiscard(self, node):
# XXX Discard means it's an expression. Perhaps this is a bad
# name.
Const
已替换为代表各种常量的几种不同类型,特别是 Num
、Str
、JoinedStr
和其他一些。所有 ast 类型的文档 can be found here.
我正在尝试将一个较旧的库转换为 Python 3. 我有 2 个部分,我不知道要用什么来替代,因为我找不到任何一个的文档功能确实 - from compiler.ast import Discard, Const
我似乎无法在 ast
库的 Python 3 中找到等效项。以下是来自 Python 2 的函数调用,请参阅此处的两个都在 isinstance()
调用中使用:
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 调用。非常感谢。
Discard
(我花了一段时间才弄清楚它的作用)现在是 Expr
(尽管这比以前包含更多内容)
我从 compiler/codegen.py:
的来源得到了这个提示 def visitDiscard(self, node):
# XXX Discard means it's an expression. Perhaps this is a bad
# name.
Const
已替换为代表各种常量的几种不同类型,特别是 Num
、Str
、JoinedStr
和其他一些。所有 ast 类型的文档 can be found here.