ast.parse的filename参数的用途是什么?
作者:互联网
该文档的内容如下:
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节点取回该文件名.或者如何使用该文件名参数.只是存根吗?
解决方法:
它在代码对象上设置co_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
标签:abstract-syntax-tree,python 来源: https://codeday.me/bug/20191123/2064642.html