Python 什么';是使用ast.parse的filename参数吗?
文件内容如下:Python 什么';是使用ast.parse的filename参数吗?,python,abstract-syntax-tree,Python,Abstract Syntax Tree,文件内容如下: ast.parse(source, filename='<unknown>', mode='exec') Equivalent to compile(source, filename, mode, ast.PyCF_ONLY_AST). compile(source, filename, mode[, flags[, dont_inherit]]) The filename argument should give the file from
ast.parse(source, filename='<unknown>', mode='exec')
Equivalent to compile(source, filename, mode, ast.PyCF_ONLY_AST).
compile(source, filename, mode[, flags[, dont_inherit]])
The filename argument should give the file from which the code was read;
pass some recognizable value if it wasn’t read from a file
('<string>' is commonly used).
ast.parse(源代码,文件名='',模式='exec')
等效于编译(源代码、文件名、模式、ast.PyCF\u ONLY\u ast)。
编译(源、文件名、模式[、标志[、不继承]])
filename参数应该给出读取代码的文件;
如果不是从文件中读取,则传递一些可识别的值
(“”是常用的)。
但是它没有告诉我如何从AST节点取回这个文件名。或者如何使用此文件名参数。它只是一个存根吗?它在代码对象上设置
co\u filename
属性,用于在回溯中显示文件名。除此之外,你传递的价值并不重要
>>> c = compile('raise Exception("spam")', 'eggs', 'exec')
>>> eval(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "eggs", line 1, in <module>
Exception: spam
c=compile('raiseexception(“spam”),'eggs','exec')
>>>评估(c)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“鸡蛋”,第1行,在
例外:垃圾邮件
因此,如果我使用
ast.PyCF\u ONLY\u ast
标志,则不会创建代码对象,也不会使用filename
参数。这似乎是一个糟糕的设计决策。