python:获取导入函数的抽象语法树? [英] python: get the abstract syntax tree of imported function?

查看:38
本文介绍了python:获取导入函数的抽象语法树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经在解释器中导入了一个 python 模块.如何在解释器中获取导入模块(以及其中的任何函数和类)的抽象语法树?我不想重新解析源文件.谢谢!

解决方案

也许你会从这个食谱中找到一些灵感:

<块引用>

输出 Python AST 的人类可读版本的函数.

或者使用compiler结合inspect(当然还是使用源码):

<预><代码>>>>导入编译器,检查>>>导入 re # 进行测试>>>compiler.parse(inspect.getsource(re))Module('支持正则表达式 (RE). 此模块提供...

Let's say I've already imported a python module in the interpreter. How can I get the abstract syntax tree of the imported module (and any functions and classes within it) within the interpreter? I don't want to have to re-parse the source files. Thanks!

解决方案

Maybe you find some inspiration in this recipe:

A function that outputs a human-readable version of a Python AST.

Or use compiler combined with inspect (which, of course, still uses the source):

>>> import compiler, inspect
>>> import re # for testing 
>>> compiler.parse(inspect.getsource(re))
Module('Support for regular expressions (RE). 

This module provides ...

这篇关于python:获取导入函数的抽象语法树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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