gpt4 book ai didi

python - 找出在 ast.py 中导入的 _ast 模块的元素

转载 作者:太空宇宙 更新时间:2023-11-04 05:46:10 24 4
gpt4 key购买 nike

我正在使用 python 3.4.3,并且正在使用 python 抽象语法树 (ast) 模块。我查看了 ast.py 模块的源代码,发现所有 ast 节点类都在 ast.py 模块导入的 _ast 模块中实现。

from _ast import *

现在,我想查看 ast 模块的元素,所以我得到了该模块中实现的所有类的名称:

import _ast
import inspect
for element in inspect.getmembers(_ast) :
print(element)

我得到了这个模块所有元素的名称:

('AST', <class '_ast.AST'>)
('Add', <class '_ast.Add'>)
('And', <class '_ast.And'>)
('Assert', <class '_ast.Assert'>)
('Assign', <class '_ast.Assign'>)
('Attribute', <class '_ast.Attribute'>)
('AugAssign', <class '_ast.AugAssign'>)
('AugLoad', <class '_ast.AugLoad'>)
('AugStore', <class '_ast.AugStore'>)
('BinOp', <class '_ast.BinOp'>)
('BitAnd', <class '_ast.BitAnd'>)
('BitOr', <class '_ast.BitOr'>)
('BitXor', <class '_ast.BitXor'>)
('BoolOp', <class '_ast.BoolOp'>)
('Break', <class '_ast.Break'>)
('Bytes', <class '_ast.Bytes'>)
('Call', <class '_ast.Call'>)
('ClassDef', <class '_ast.ClassDef'>)
('Compare', <class '_ast.Compare'>)
('Continue', <class '_ast.Continue'>)
('Del', <class '_ast.Del'>)
('Delete', <class '_ast.Delete'>)
('Dict', <class '_ast.Dict'>)
('DictComp', <class '_ast.DictComp'>)
('Div', <class '_ast.Div'>)
('Ellipsis', <class '_ast.Ellipsis'>)
('Eq', <class '_ast.Eq'>)
('ExceptHandler', <class '_ast.ExceptHandler'>)
('Expr', <class '_ast.Expr'>)
('Expression', <class '_ast.Expression'>)
('ExtSlice', <class '_ast.ExtSlice'>)
('FloorDiv', <class '_ast.FloorDiv'>)
('For', <class '_ast.For'>)
('FunctionDef', <class '_ast.FunctionDef'>)
('GeneratorExp', <class '_ast.GeneratorExp'>)
('Global', <class '_ast.Global'>)
('Gt', <class '_ast.Gt'>)
('GtE', <class '_ast.GtE'>)
('If', <class '_ast.If'>)
('IfExp', <class '_ast.IfExp'>)
('Import', <class '_ast.Import'>)
('ImportFrom', <class '_ast.ImportFrom'>)
('In', <class '_ast.In'>)
('Index', <class '_ast.Index'>)
('Interactive', <class '_ast.Interactive'>)
('Invert', <class '_ast.Invert'>)
('Is', <class '_ast.Is'>)
('IsNot', <class '_ast.IsNot'>)
('LShift', <class '_ast.LShift'>)
('Lambda', <class '_ast.Lambda'>)
('List', <class '_ast.List'>)
('ListComp', <class '_ast.ListComp'>)
('Load', <class '_ast.Load'>)
('Lt', <class '_ast.Lt'>)
('LtE', <class '_ast.LtE'>)
('Mod', <class '_ast.Mod'>)
('Module', <class '_ast.Module'>)
('Mult', <class '_ast.Mult'>)
('Name', <class '_ast.Name'>)
('NameConstant', <class '_ast.NameConstant'>)
('Nonlocal', <class '_ast.Nonlocal'>)
('Not', <class '_ast.Not'>)
('NotEq', <class '_ast.NotEq'>)
('NotIn', <class '_ast.NotIn'>)
('Num', <class '_ast.Num'>)
('Or', <class '_ast.Or'>)
('Param', <class '_ast.Param'>)
('Pass', <class '_ast.Pass'>)
('Pow', <class '_ast.Pow'>)
('PyCF_ONLY_AST', 1024)
('RShift', <class '_ast.RShift'>)
('Raise', <class '_ast.Raise'>)
('Return', <class '_ast.Return'>)
('Set', <class '_ast.Set'>)
('SetComp', <class '_ast.SetComp'>)
('Slice', <class '_ast.Slice'>)
('Starred', <class '_ast.Starred'>)
('Store', <class '_ast.Store'>)
('Str', <class '_ast.Str'>)
('Sub', <class '_ast.Sub'>)
('Subscript', <class '_ast.Subscript'>)
('Suite', <class '_ast.Suite'>)
('Try', <class '_ast.Try'>)
('Tuple', <class '_ast.Tuple'>)
('UAdd', <class '_ast.UAdd'>)
('USub', <class '_ast.USub'>)
('UnaryOp', <class '_ast.UnaryOp'>)
('While', <class '_ast.While'>)
('With', <class '_ast.With'>)
('Yield', <class '_ast.Yield'>)
('YieldFrom', <class '_ast.YieldFrom'>)
('__doc__', None)
('__loader__', <class '_frozen_importlib.BuiltinImporter'>)
('__name__', '_ast')
('__package__', '')
('__spec__', ModuleSpec(name='_ast', loader=<class '_frozen_importlib.BuiltinImporter'>, origin='built-in'))
('alias', <class '_ast.alias'>)
('arg', <class '_ast.arg'>)
('arguments', <class '_ast.arguments'>)
('boolop', <class '_ast.boolop'>)
('cmpop', <class '_ast.cmpop'>)
('comprehension', <class '_ast.comprehension'>)
('excepthandler', <class '_ast.excepthandler'>)
('expr', <class '_ast.expr'>)
('expr_context', <class '_ast.expr_context'>)
('keyword', <class '_ast.keyword'>)
('mod', <class '_ast.mod'>)
('operator', <class '_ast.operator'>)
('slice', <class '_ast.slice'>)
('stmt', <class '_ast.stmt'>)
('unaryop', <class '_ast.unaryop'>)
('withitem', <class '_ast.withitem'>)

现在,我想看看这些类中的每一个是如何在 _ast 模块中实现的。但是当我尝试使用 inspect.getsource 方法获取源文件时,出现错误:

>>inspect.getsource(_ast)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
inspect.getsource(_ast)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 836, in getsource
lines, lnum = getsourcelines(object)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 825, in getsourcelines
lines, lnum = findsource(object)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 655, in findsource
file = getsourcefile(object)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 571, in getsourcefile
filename = getfile(object)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 518, in getfile
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module '_ast' (built-in)> is a built-in module

为什么会出现这个错误。我如何获得所有这些类的源代码?

提前致谢

最佳答案

你不能那样做(不是 _ast 模块,它是一个 built-in 模块)。明确说明in the documentations -

inspect.getsourcefile(object)

Return the name of the Python source file in which an object was defined. This will fail with a TypeError if the object is a built-in module, class, or function.

(强调我的)

并且 inspect.getsource() 在内部使用 inspect.getsourcefile() ,从 Traceback 可以明显看出。

由于 _ast 是内置模块,Example -

>>> import _ast
>>> _ast
<module '_ast' (built-in)>

您无法通过该方法获取其来源。如果真想看源码,Python的源码是开源的,可用here .我认为您要获取源代码的模块完全是用 C 编写的。

关于python - 找出在 ast.py 中导入的 _ast 模块的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32218509/

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