我正在使用python 3.4.3,我正在使用python抽象語法樹(ast)模塊。我查看了ast.py模塊的源代碼,發現ast_py模塊導入了_ast模塊中的所有ast節點類。現在找出_ast模塊的元素,它被導入ast.py
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
爲什麼我得到這個錯誤。我如何獲得所有這些課程的來源?
在此先感謝
@ Anand S Kumar因此,這個問題,我無法找到模塊位於python源代碼分佈的位置。我如何找到它?是否有某種以'_'開頭的文件名的命名約定? – ironstein
我不知道任何命名約定,但我認爲'_ast'模塊在Python \ ast.c'文件中。我不是100%確定,但似乎是這樣。 –
非常感謝,非常感謝 – ironstein