找出_ast模块的元素,该模块已导入ast.py [英] find out elements of _ast module, which is imported in ast.py

查看:102
本文介绍了找出_ast模块的元素,该模块已导入ast.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using python 3.4.3 and I am working with the python abstract syntax tree(ast) module. I looked at the source of the ast.py module and I found out that all the ast node classes are implemented in the _ast module, which the ast.py module imports.

from _ast import *

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

Now, I wanted to look at the elements of the ast module, so I got the name of all the classes implemented in this module :

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

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

I got the names of all elements of this module :

('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方法获取源文件时,出现错误:

Now, I want to look at how each of these classes is implemented in the _ast module. But when I try to get the source file using the inspect.getsource method, I get an error :

>>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

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

why do i get this error. And how do i get the source of all these classes ?

预先感谢

推荐答案

您不能这样做(不适用于_ast模块,它是built-in模块).正如文档中 -

You cannot do that (not for _ast module , which is a built-in module). As clearly stated in the documentations -

inspect.getsourcefile(object)

返回在其中定义对象的Python源文件的名称. 如果对象是内置模块,类或函数,则将失败并显示TypeError.

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(),从追溯中可以明显看出.

And inspect.getsource() internally uses inspect.getsourcefile() , as evident from the Traceback.

由于_ast是内置模块,因此示例-

Since _ast is a built-in module, Example -

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

您无法通过该方法获取其来源.如果您真的想看一下源代码,那么Python的源代码是开放源代码,可以使用这里.我认为您要获取源代码的模块完全用C编写.

You cannot get its source through that method. If you really want to take a look at the source code, the source code for Python is open source and available here . I think the module you are trying to get source for is written completely in C .

这篇关于找出_ast模块的元素,该模块已导入ast.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆