gpt4 book ai didi

python - ast.parse的文件名参数有什么用?

转载 作者:太空宇宙 更新时间:2023-11-03 11:06:38 25 4
gpt4 key购买 nike

文档内容如下:

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

最佳答案

它在代码对象上设置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

关于python - ast.parse的文件名参数有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17571727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com