5

文档内容如下:

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 节点取回这个文件名。或者如何使用这个文件名参数。它只是一个存根吗?

4

1 回答 1

3

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
于 2013-07-10T13:27:20.297 回答